|
@@ -5,24 +5,19 @@ import org.apache.http.client.config.RequestConfig;
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
import org.apache.http.client.methods.HttpGet;
|
|
import org.apache.http.client.methods.HttpGet;
|
|
import org.apache.http.client.methods.HttpPost;
|
|
import org.apache.http.client.methods.HttpPost;
|
|
-import org.apache.http.config.Registry;
|
|
|
|
-import org.apache.http.config.RegistryBuilder;
|
|
|
|
-import org.apache.http.conn.socket.ConnectionSocketFactory;
|
|
|
|
-import org.apache.http.conn.socket.PlainConnectionSocketFactory;
|
|
|
|
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
|
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
import org.apache.http.entity.StringEntity;
|
|
import org.apache.http.entity.StringEntity;
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
import org.apache.http.impl.client.HttpClients;
|
|
import org.apache.http.impl.client.HttpClients;
|
|
-import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
|
|
|
-import org.apache.http.ssl.SSLContextBuilder;
|
|
|
|
import org.apache.http.util.EntityUtils;
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
|
|
|
|
+import javax.net.ssl.SSLContext;
|
|
|
|
+import javax.net.ssl.TrustManager;
|
|
|
|
+import javax.net.ssl.X509TrustManager;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStream;
|
|
-import java.security.KeyManagementException;
|
|
|
|
-import java.security.KeyStoreException;
|
|
|
|
-import java.security.NoSuchAlgorithmException;
|
|
|
|
|
|
+import java.security.cert.X509Certificate;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -35,22 +30,56 @@ import java.util.Map;
|
|
@Slf4j
|
|
@Slf4j
|
|
public class HttpUtil {
|
|
public class HttpUtil {
|
|
|
|
|
|
- public static CloseableHttpClient getHttpClient() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
|
|
|
|
|
|
+// public static CloseableHttpClient getHttpClient() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
|
|
|
|
+//
|
|
|
|
+// PlainConnectionSocketFactory plainConnectionSocketFactory = new PlainConnectionSocketFactory();
|
|
|
|
+// SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
|
|
|
|
+// SSLContextBuilder.create().loadTrustMaterial(null, (x509Certificates, s) -> true).build(), // 全部信任 不做身份鉴定
|
|
|
|
+// new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2"},
|
|
|
|
+// null,
|
|
|
|
+// NoopHostnameVerifier.INSTANCE);
|
|
|
|
+// Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
|
|
|
|
+// .register("http", plainConnectionSocketFactory)
|
|
|
|
+// .register("https", sslConnectionSocketFactory)
|
|
|
|
+// .build();
|
|
|
|
+// PoolingHttpClientConnectionManager pool = new PoolingHttpClientConnectionManager(registry);
|
|
|
|
+// pool.setMaxTotal(300);
|
|
|
|
+// pool.setDefaultMaxPerRoute(300);
|
|
|
|
+// return HttpClients.custom().setConnectionManager(pool).build();
|
|
|
|
+// }
|
|
|
|
|
|
- PlainConnectionSocketFactory plainConnectionSocketFactory = new PlainConnectionSocketFactory();
|
|
|
|
- SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
|
|
|
|
- SSLContextBuilder.create().loadTrustMaterial(null, (x509Certificates, s) -> true).build(), // 全部信任 不做身份鉴定
|
|
|
|
- new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2"},
|
|
|
|
- null,
|
|
|
|
- NoopHostnameVerifier.INSTANCE);
|
|
|
|
- Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
|
|
|
|
- .register("http", plainConnectionSocketFactory)
|
|
|
|
- .register("https", sslConnectionSocketFactory)
|
|
|
|
- .build();
|
|
|
|
- PoolingHttpClientConnectionManager pool = new PoolingHttpClientConnectionManager(registry);
|
|
|
|
- pool.setMaxTotal(300);
|
|
|
|
- pool.setDefaultMaxPerRoute(300);
|
|
|
|
- return HttpClients.custom().setConnectionManager(pool).build();
|
|
|
|
|
|
+ public static CloseableHttpClient getHttpClient() {
|
|
|
|
+ return getCloseableHttpClient();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private static CloseableHttpClient getCloseableHttpClient() {
|
|
|
|
+ try {
|
|
|
|
+ SSLContext sslContext = SSLContext.getInstance("TLS");
|
|
|
|
+ X509TrustManager x509TrustManager = new X509TrustManager() {
|
|
|
|
+ @Override
|
|
|
|
+ public X509Certificate[] getAcceptedIssuers() {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void checkClientTrusted(X509Certificate[] arg0, String arg1) {
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void checkServerTrusted(X509Certificate[] arg0, String arg1) {
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+ sslContext.init(null, new TrustManager[]{x509TrustManager}, null);
|
|
|
|
+ SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
|
|
|
|
+// PoolingHttpClientConnectionManager pool = new PoolingHttpClientConnectionManager();
|
|
|
|
+// pool.setMaxTotal(300);
|
|
|
|
+// pool.setDefaultMaxPerRoute(300);
|
|
|
|
+// return HttpClients.custom().setConnectionManager(pool).setSSLSocketFactory(sslConnectionSocketFactory).build();
|
|
|
|
+ return HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory).build();
|
|
|
|
+ } catch (Exception exception) {
|
|
|
|
+ throw new RuntimeException("获取 httpClient 失败。", exception);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
public static RequestConfig getRequestConfig() {
|
|
public static RequestConfig getRequestConfig() {
|
|
@@ -86,7 +115,7 @@ public class HttpUtil {
|
|
result = EntityUtils.toString(response.getEntity());
|
|
result = EntityUtils.toString(response.getEntity());
|
|
} else {
|
|
} else {
|
|
log.error(response.getEntity().toString());
|
|
log.error(response.getEntity().toString());
|
|
- throw new RuntimeException("get() 请求错误:" + response);
|
|
|
|
|
|
+ throw new RuntimeException("请求错误:" + response);
|
|
}
|
|
}
|
|
} catch (org.apache.http.conn.ConnectTimeoutException cte) {
|
|
} catch (org.apache.http.conn.ConnectTimeoutException cte) {
|
|
log.debug(url + " 请求超时。", cte);
|
|
log.debug(url + " 请求超时。", cte);
|