|
|
|
@ -1,42 +1,33 @@
@@ -1,42 +1,33 @@
|
|
|
|
|
package vip.xumy.core.http; |
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.security.cert.CertificateException; |
|
|
|
|
import java.security.cert.X509Certificate; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
import javax.net.ssl.SSLContext; |
|
|
|
|
import javax.net.ssl.TrustManager; |
|
|
|
|
import javax.net.ssl.X509TrustManager; |
|
|
|
|
|
|
|
|
|
import org.apache.http.Header; |
|
|
|
|
import org.apache.http.NameValuePair; |
|
|
|
|
import org.apache.http.client.CookieStore; |
|
|
|
|
import org.apache.http.client.config.RequestConfig; |
|
|
|
|
import org.apache.http.client.entity.UrlEncodedFormEntity; |
|
|
|
|
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.client.methods.HttpPut; |
|
|
|
|
import org.apache.http.client.utils.URIBuilder; |
|
|
|
|
import org.apache.http.conn.ssl.NoopHostnameVerifier; |
|
|
|
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
|
|
|
|
import org.apache.http.entity.StringEntity; |
|
|
|
|
import org.apache.http.impl.client.BasicCookieStore; |
|
|
|
|
import org.apache.http.impl.client.CloseableHttpClient; |
|
|
|
|
import org.apache.http.impl.client.HttpClientBuilder; |
|
|
|
|
import org.apache.http.impl.client.HttpClients; |
|
|
|
|
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; |
|
|
|
|
import org.apache.http.impl.cookie.BasicClientCookie; |
|
|
|
|
import org.apache.http.message.BasicHeader; |
|
|
|
|
import org.apache.http.message.BasicNameValuePair; |
|
|
|
|
import org.apache.http.util.EntityUtils; |
|
|
|
|
|
|
|
|
|
import lombok.extern.log4j.Log4j2; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Ownership belongs to the company author:mengyxu date:2019年5月25日 |
|
|
|
|
/** All rights reserved |
|
|
|
|
* author:mengyxu |
|
|
|
|
* date:2019年5月25日 |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
@Log4j2 |
|
|
|
@ -46,40 +37,37 @@ public class HttpService {
@@ -46,40 +37,37 @@ public class HttpService {
|
|
|
|
|
|
|
|
|
|
private static final PoolingHttpClientConnectionManager cm; |
|
|
|
|
|
|
|
|
|
private static RequestConfig config; |
|
|
|
|
private static final RequestConfig config; |
|
|
|
|
|
|
|
|
|
private static CookieStore cookieStore = new BasicCookieStore(); |
|
|
|
|
|
|
|
|
|
static { |
|
|
|
|
static{ |
|
|
|
|
cm = new PoolingHttpClientConnectionManager(); |
|
|
|
|
cm.setMaxTotal(200); |
|
|
|
|
cm.setDefaultMaxPerRoute(20); |
|
|
|
|
|
|
|
|
|
config = RequestConfig.custom().setConnectTimeout(100000).setConnectionRequestTimeout(50000) |
|
|
|
|
.setSocketTimeout(100000).setStaleConnectionCheckEnabled(true).build(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void setRedirectsEnabled(boolean enable) { |
|
|
|
|
config = RequestConfig.custom().setConnectTimeout(100000).setConnectionRequestTimeout(50000) |
|
|
|
|
.setSocketTimeout(100000).setRedirectsEnabled(enable).setStaleConnectionCheckEnabled(true).build(); |
|
|
|
|
config = RequestConfig.custom().setConnectTimeout(100000) |
|
|
|
|
.setConnectionRequestTimeout(50000).setSocketTimeout(100000) |
|
|
|
|
.setStaleConnectionCheckEnabled(true).build(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 创建请求所携带cookie |
|
|
|
|
* |
|
|
|
|
* 设置请求所携带cookie |
|
|
|
|
* @param cookies |
|
|
|
|
*/ |
|
|
|
|
public static BasicCookieStore createCookies(Map<String, String> cookies, String domain) { |
|
|
|
|
BasicCookieStore cookieStore = new BasicCookieStore(); |
|
|
|
|
if (cookies != null) { |
|
|
|
|
public static void setCookies(Map<String, String> cookies) { |
|
|
|
|
cookieStore = new BasicCookieStore(); |
|
|
|
|
if(cookies != null) { |
|
|
|
|
for (String key : cookies.keySet()) { |
|
|
|
|
String value = cookies.get(key); |
|
|
|
|
BasicClientCookie clientCookie = new BasicClientCookie(key, value); |
|
|
|
|
clientCookie.setVersion(0); |
|
|
|
|
clientCookie.setSecure(false); |
|
|
|
|
clientCookie.setDomain(domain); |
|
|
|
|
clientCookie.setDomain("."); |
|
|
|
|
clientCookie.setPath("/"); |
|
|
|
|
cookieStore.addCookie(clientCookie); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return cookieStore; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -89,7 +77,7 @@ public class HttpService {
@@ -89,7 +77,7 @@ public class HttpService {
|
|
|
|
|
* @param map |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public static HttpResult doGet(String url, Map<String, Object> param, BasicCookieStore cookieStore) { |
|
|
|
|
public static HttpResult doGet(String url, Map<String, Object> param) { |
|
|
|
|
try { |
|
|
|
|
// 1.创建URIBuilder
|
|
|
|
|
URIBuilder uriBuilder = new URIBuilder(url); |
|
|
|
@ -107,72 +95,37 @@ public class HttpService {
@@ -107,72 +95,37 @@ public class HttpService {
|
|
|
|
|
HttpGet httpGet = new HttpGet(uriBuilder.build()); |
|
|
|
|
httpGet.setConfig(config); |
|
|
|
|
// 4.使用httpClient发起请求
|
|
|
|
|
CloseableHttpResponse response = HttpClients.custom().setDefaultCookieStore(cookieStore) |
|
|
|
|
.setConnectionManager(cm).build().execute(httpGet); |
|
|
|
|
Header[] allHeaders = httpGet.getAllHeaders(); |
|
|
|
|
// 5.解析返回结果,封装返回对象httpResult
|
|
|
|
|
return getResult(response); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
log.error("http request failed", e); |
|
|
|
|
return new HttpResult(400, "http request failed"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static HttpResult doGet(String url) { |
|
|
|
|
return doGet(url, null, null); |
|
|
|
|
} |
|
|
|
|
CloseableHttpResponse response = HttpClients.custom().setDefaultCookieStore(cookieStore).setConnectionManager(cm).build() |
|
|
|
|
.execute(httpGet); |
|
|
|
|
|
|
|
|
|
public static HttpResult doGet(String url, BasicCookieStore cookieStore) { |
|
|
|
|
return doGet(url, null, cookieStore); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static HttpResult doPut(String url, Map<String, Object> param, BasicCookieStore cookieStore) { |
|
|
|
|
try { |
|
|
|
|
// 1.创建URIBuilder
|
|
|
|
|
URIBuilder uriBuilder = new URIBuilder(url); |
|
|
|
|
|
|
|
|
|
// 2.设置请求参数
|
|
|
|
|
if (param != null) { |
|
|
|
|
// 遍历请求参数
|
|
|
|
|
for (Map.Entry<String, Object> entry : param.entrySet()) { |
|
|
|
|
// 封装请求参数
|
|
|
|
|
uriBuilder.setParameter(entry.getKey(), entry.getValue().toString()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 3.创建请求对象httpGet
|
|
|
|
|
HttpPut httpPut = new HttpPut(uriBuilder.build()); |
|
|
|
|
httpPut.setConfig(config); |
|
|
|
|
// 4.使用httpClient发起请求
|
|
|
|
|
CloseableHttpResponse response = HttpClients.custom().setDefaultCookieStore(cookieStore) |
|
|
|
|
.setConnectionManager(cm).build().execute(httpPut); |
|
|
|
|
Header[] allHeaders = httpPut.getAllHeaders(); |
|
|
|
|
// 5.解析返回结果,封装返回对象httpResult
|
|
|
|
|
return getResult(response); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
log.error("http request failed", e); |
|
|
|
|
return new HttpResult(400, "http request failed"); |
|
|
|
|
log.error("http request failed",e); |
|
|
|
|
return new HttpResult(500, "http request failed"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static HttpResult doPut(String url) { |
|
|
|
|
return doPut(url, null, null); |
|
|
|
|
/** |
|
|
|
|
* 不带参数的get |
|
|
|
|
* |
|
|
|
|
* @param url |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public static HttpResult doGet(String url) { |
|
|
|
|
return doGet(url, null); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static HttpResult doPut(String url, BasicCookieStore cookieStore) { |
|
|
|
|
return doPut(url, null, cookieStore); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 带参数的post请求 |
|
|
|
|
* |
|
|
|
|
* @param url |
|
|
|
|
* @param map |
|
|
|
|
* @return @ |
|
|
|
|
* @return |
|
|
|
|
* @ |
|
|
|
|
*/ |
|
|
|
|
public static HttpResult doPost(String url, Map<String, Object> param, BasicCookieStore cookieStore) { |
|
|
|
|
public static HttpResult doPost(String url, Map<String, Object> param) { |
|
|
|
|
try { |
|
|
|
|
// 1. 声明httppost
|
|
|
|
|
HttpPost httpPost = new HttpPost(url); |
|
|
|
@ -197,13 +150,13 @@ public class HttpService {
@@ -197,13 +150,13 @@ public class HttpService {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 4. 使用Httpclient发起请求
|
|
|
|
|
CloseableHttpResponse response = HttpClients.custom().setDefaultCookieStore(cookieStore) |
|
|
|
|
.setConnectionManager(cm).build().execute(httpPost); |
|
|
|
|
CloseableHttpResponse response = HttpClients.custom().setDefaultCookieStore(cookieStore).setConnectionManager(cm).build() |
|
|
|
|
.execute(httpPost); |
|
|
|
|
|
|
|
|
|
return getResult(response); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error("http request failed", e); |
|
|
|
|
return new HttpResult(400, "http request failed"); |
|
|
|
|
log.error("http request failed",e); |
|
|
|
|
return new HttpResult(500, "http request failed"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -212,7 +165,8 @@ public class HttpService {
@@ -212,7 +165,8 @@ public class HttpService {
|
|
|
|
|
* |
|
|
|
|
* @param url 请求地址 |
|
|
|
|
* @param params json格式参数 |
|
|
|
|
* @return @ |
|
|
|
|
* @return |
|
|
|
|
* @ |
|
|
|
|
*/ |
|
|
|
|
public static HttpResult postWithJson(String url, String params, Map<String, String> headers) { |
|
|
|
|
try { |
|
|
|
@ -224,8 +178,8 @@ public class HttpService {
@@ -224,8 +178,8 @@ public class HttpService {
|
|
|
|
|
StringEntity entity = new StringEntity(params, charSet); |
|
|
|
|
httpPost.setEntity(entity); |
|
|
|
|
|
|
|
|
|
// 设置http请求头
|
|
|
|
|
if (headers != null) { |
|
|
|
|
//设置http请求头
|
|
|
|
|
if(headers != null) { |
|
|
|
|
for (String key : headers.keySet()) { |
|
|
|
|
httpPost.setHeader(key, headers.get(key)); |
|
|
|
|
} |
|
|
|
@ -237,57 +191,9 @@ public class HttpService {
@@ -237,57 +191,9 @@ public class HttpService {
|
|
|
|
|
// 5. 解析返回数据,封装HttpResult
|
|
|
|
|
return getResult(response); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error("http request failed", e); |
|
|
|
|
return new HttpResult(400, "http request failed"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static HttpResult postHttps(String url, String data) throws Exception { |
|
|
|
|
return postHttps(url, data, null); |
|
|
|
|
log.error("http request failed",e); |
|
|
|
|
return new HttpResult(500, "http request failed"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static HttpResult postHttps(String url, String data, Map<String, String> headers) throws Exception { |
|
|
|
|
try { |
|
|
|
|
HttpPost httpPost = new HttpPost(url); |
|
|
|
|
httpPost.addHeader("Content-Type", "application/json"); |
|
|
|
|
if (headers != null) { |
|
|
|
|
for (String key : headers.keySet()) { |
|
|
|
|
httpPost.addHeader(key, headers.get(key)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
StringEntity se = new StringEntity(data); |
|
|
|
|
se.setContentType("text/json"); |
|
|
|
|
se.setContentEncoding(new BasicHeader("Content-Type", "application/json")); |
|
|
|
|
httpPost.setEntity(se); |
|
|
|
|
CloseableHttpResponse response = httpClientTrustingAllSSLCerts().build().execute(httpPost); |
|
|
|
|
// 5. 解析返回数据,封装HttpResult
|
|
|
|
|
return getResult(response); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error("http request failed", e); |
|
|
|
|
return new HttpResult(400, "http request failed"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static HttpClientBuilder httpClientTrustingAllSSLCerts() throws Exception { |
|
|
|
|
TrustManager[] trustAllCertificates = new TrustManager[] { new X509TrustManager() { |
|
|
|
|
@Override |
|
|
|
|
public X509Certificate[] getAcceptedIssuers() { |
|
|
|
|
return null; // Not relevant.
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { |
|
|
|
|
} |
|
|
|
|
} }; |
|
|
|
|
SSLContext sc = SSLContext.getInstance("SSL"); |
|
|
|
|
sc.init(null, trustAllCertificates, new java.security.SecureRandom()); |
|
|
|
|
SSLConnectionSocketFactory scsf = new SSLConnectionSocketFactory(sc, NoopHostnameVerifier.INSTANCE); |
|
|
|
|
return HttpClients.custom().setSSLSocketFactory(scsf); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static HttpResult getResult(CloseableHttpResponse response) throws IOException { |
|
|
|
@ -296,33 +202,11 @@ public class HttpService {
@@ -296,33 +202,11 @@ public class HttpService {
|
|
|
|
|
|
|
|
|
|
// 响应体内容
|
|
|
|
|
String body = null; |
|
|
|
|
String contentType = null; |
|
|
|
|
if (response.getEntity() != null) { |
|
|
|
|
body = EntityUtils.toString(response.getEntity(), ENCODING_UTF8); |
|
|
|
|
} |
|
|
|
|
Header[] types = response.getHeaders("content-type"); |
|
|
|
|
if (types != null && types.length > 0) { |
|
|
|
|
contentType = types[0].getValue(); |
|
|
|
|
} |
|
|
|
|
HttpResult httpResult = new HttpResult(code, body, contentType); |
|
|
|
|
|
|
|
|
|
// cookie
|
|
|
|
|
Header[] headers = response.getHeaders("Set-Cookie"); |
|
|
|
|
if (headers != null && headers.length > 0) { |
|
|
|
|
Map<String, String> cookies = new HashMap<>(); |
|
|
|
|
for (Header header : headers) { |
|
|
|
|
String[] arr = header.getValue().split(";"); |
|
|
|
|
for (String cookie : arr) { |
|
|
|
|
int index = cookie.indexOf("="); |
|
|
|
|
if (index != -1) { |
|
|
|
|
cookies.put(cookie.substring(0, index), cookie.substring(index + 1)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
httpResult.setCookies(cookies); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return httpResult; |
|
|
|
|
return new HttpResult(code, body); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -332,15 +216,15 @@ public class HttpService {
@@ -332,15 +216,15 @@ public class HttpService {
|
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public static HttpResult doPost(String url) { |
|
|
|
|
return doPost(url, null, null); |
|
|
|
|
return doPost(url, null); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void doPostAsync(final String url, final Map<String, Object> param) { |
|
|
|
|
public static void doPostAsync(final String url, final Map<String, Object> param){ |
|
|
|
|
new Thread(new Runnable() { |
|
|
|
|
@Override |
|
|
|
|
public void run() { |
|
|
|
|
try { |
|
|
|
|
doPost(url, param, null); |
|
|
|
|
doPost(url, param); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|