吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2301|回复: 9
收起左侧

[求助] java HttpClient模拟登陆一直401

[复制链接]
猫南北爱上狗东西 发表于 2019-12-19 22:52
本帖最后由 猫南北爱上狗东西 于 2019-12-19 22:56 编辑

java   HttpClient模拟登陆一直401

dopost函数2

dopost函数2

登录页面

登录页面

dopost函数

dopost函数

主函数

主函数

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Yoona520 发表于 2019-12-19 23:12
用OKHttp
wl18122311198 发表于 2019-12-20 00:04
[Java] 纯文本查看 复制代码
package com.util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.car.util.DownloadImg;
import com.car.util.GetImgSrc;
import com.car.util.HTMLParseUtil;
import com.car.util.JsoupUtil;
import com.car.util.StringUtils;

import lombok.Data;
import org.apache.http.Consts;
import org.apache.http.Header;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
import org.json.XML;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;

public class HTTPUtils {
	
    private static RequestConfig config;

    public HTTPUtils(){
        config = RequestConfig.custom()
                .setConnectionRequestTimeout(3000)
                .setConnectTimeout(3000)
                .setSocketTimeout(3000)
                .build();
    }

    private static final String request_url = "https://www.baidu.com/";

    public static void main(String[] args) throws Exception{
        Response response = new HTTPUtils().get(request_url,"");
        System.out.println(response.responseBody);
    }


    public HTTPUtils(int connectionRequestTimeout, int connectTimeout, int socketTimeout){
        config = RequestConfig.custom()
                .setConnectionRequestTimeout(connectionRequestTimeout)
                .setConnectTimeout(connectTimeout)
                .setSocketTimeout(socketTimeout)
                .build();
    }


    public Response post(String url, String header, String requestBody) throws IOException {
        CloseableHttpClient httpclient = buildSSLCloseableHttpClient(url);
        HttpPost httppost = new HttpPost(url);
        httppost.setConfig(config);
        if (header != null && !header.equals("")) {
            for (Map.Entry<String, String> entry : getRequestHeader(header).entrySet()) {
                httppost.setHeader(entry.getKey(), entry.getValue());
            }
        }
        httppost.setEntity(new StringEntity(requestBody));
        CloseableHttpResponse response = httpclient.execute(httppost);
        return getResponse(response);
    }


    public Response get(String url, String header) throws IOException {
        CloseableHttpClient httpclient = buildSSLCloseableHttpClient(url);
        HttpGet httpget = new HttpGet(url);
        httpget.setConfig(config);
        if (header != null && !header.equals("")) {
            for (Map.Entry<String, String> entry : getRequestHeader(header).entrySet()) {
                httpget.setHeader(entry.getKey(), entry.getValue());
            }
        }
        CloseableHttpResponse response = httpclient.execute(httpget);
        return getResponse(response);
    }


    private Map<String, String> getRequestHeader(String header){
        Map<String, String> headerMap = new HashMap<String, String>();
        JSONArray headerArray = JSONArray.parseArray(header);
        for (int i=0; i<headerArray.size(); i++){
            JSONObject headerObject = headerArray.getJSONObject(i);
            for (String key : headerObject.keySet()){
                headerMap.put(key, headerObject.getString(key));
            }
        }
        return headerMap;
    }


    private Map<String, String> getResponseHeader(Header[] headers){
        Map<String, String> headerMap = new HashMap<String, String>();
        for (Header header : headers) {
            headerMap.put(header.getName(), header.getValue());
        }
        return headerMap;
    }


    private CloseableHttpClient buildSSLCloseableHttpClient(String url)  {
        SSLContext sslContext = null;
        try {
            sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                        public boolean isTrusted(X509Certificate[] chain, String authType) {
                            return true;
                        }
                    }).build();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                sslContext, new String[] { "TLSv1" }, null,
                SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        return url.startsWith("https:") ? HttpClients.custom().setSSLSocketFactory(sslsf).build() : HttpClients.createDefault();
    }


    private Response getResponse(CloseableHttpResponse response){
        Response res = null;
        try {
            String result = EntityUtils.toString(response.getEntity(), Consts.UTF_8);
            res = new Response();
            res.setResponseCode(response.getStatusLine().getStatusCode());
            res.setResponseHeader(getResponseHeader(response.getAllHeaders()));
            res.setResponseBody(result);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return res;
    }

    /**
     * json to xml
     * [url=home.php?mod=space&uid=952169]@Param[/url] json String
     * @return
     */
    public String json2xml(String json) {
        org.json.JSONObject jsonObj = new org.json.JSONObject(json);
        return "<xml>" + XML.toString(jsonObj) + "</xml>";
    }

    /**
     * xml to json
     * @param xml String
     * @return
     */
    public String xml2json(String xml) {
        org.json.JSONObject xmlJSONObj = XML.toJSONObject(xml.replace("<xml>", "").replace("</xml>", ""));
        return xmlJSONObj.toString();
    }

    @Data
    public class Response{
        public int responseCode;
        public Map<String, String> responseHeader;
        public Object responseBody;
    }

}


试试这个
2Burhero 发表于 2019-12-20 02:21
rose520rain 发表于 2019-12-20 07:01
反正我也看不懂,点赞就行了
smdzj 发表于 2019-12-20 08:44
401是没权限,那你账号密码输入对了吗?能返回说明能通
homurachyan 发表于 2019-12-20 08:55
可能因为是访问HTTPS,createSSLClientDefault的实例需要设置对于证书的信任,可以设置为全通过
 楼主| 猫南北爱上狗东西 发表于 2019-12-20 09:22
homurachyan 发表于 2019-12-20 08:55
可能因为是访问HTTPS,createSSLClientDefault的实例需要设置对于证书的信任,可以设置为全通过

已经设置了
 楼主| 猫南北爱上狗东西 发表于 2019-12-20 09:23
smdzj 发表于 2019-12-20 08:44
401是没权限,那你账号密码输入对了吗?能返回说明能通

账号密码是对的
 楼主| 猫南北爱上狗东西 发表于 2019-12-20 09:24
wl18122311198 发表于 2019-12-20 00:04
[mw_shl_code=java,true]package com.util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba ...

这个参数怎么传啊
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-26 22:39

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表