日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java http 401_java HttpClient模拟登陆一直401

發布時間:2023/12/16 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java http 401_java HttpClient模拟登陆一直401 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

[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 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 entry : getRequestHeader(header).entrySet()) {

httpget.setHeader(entry.getKey(), entry.getValue());

}

}

CloseableHttpResponse response = httpclient.execute(httpget);

return getResponse(response);

}

private Map getRequestHeader(String header){

Map headerMap = new HashMap();

JSONArray headerArray = JSONArray.parseArray(header);

for (int i=0; i

JSONObject headerObject = headerArray.getJSONObject(i);

for (String key : headerObject.keySet()){

headerMap.put(key, headerObject.getString(key));

}

}

return headerMap;

}

private Map getResponseHeader(Header[] headers){

Map headerMap = new HashMap();

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.toString(jsonObj) + "";

}

/**

* xml to json

* @param xml String

* @return

*/

public String xml2json(String xml) {

org.json.JSONObject xmlJSONObj = XML.toJSONObject(xml.replace("", "").replace("", ""));

return xmlJSONObj.toString();

}

@Data

public class Response{

public int responseCode;

public Map responseHeader;

public Object responseBody;

}

}

總結

以上是生活随笔為你收集整理的java http 401_java HttpClient模拟登陆一直401的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。