|
@@ -9,13 +9,39 @@ import java.net.ConnectException;
|
|
|
import java.net.SocketTimeoutException;
|
|
|
import java.net.URL;
|
|
|
import java.net.URLConnection;
|
|
|
+import java.security.KeyManagementException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.security.cert.CertificateException;
|
|
|
import java.security.cert.X509Certificate;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import javax.net.ssl.HostnameVerifier;
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
import javax.net.ssl.SSLSession;
|
|
|
import javax.net.ssl.TrustManager;
|
|
|
import javax.net.ssl.X509TrustManager;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import org.apache.commons.lang.exception.ExceptionUtils;
|
|
|
+import org.apache.http.Header;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+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.SSLConnectionSocketFactory;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
|
|
+import org.apache.http.message.BasicHeader;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import com.ydd.common.constant.Constants;
|
|
@@ -29,6 +55,61 @@ public class HttpUtils
|
|
|
{
|
|
|
private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);
|
|
|
|
|
|
+ private static final String CHARSET = "utf-8";
|
|
|
+
|
|
|
+ private final static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).setConnectionRequestTimeout(60000).build();
|
|
|
+ private static PoolingHttpClientConnectionManager manager;
|
|
|
+ private static SSLContext sslcontext;
|
|
|
+
|
|
|
+ static {
|
|
|
+ sslcontext = createIgnoreVerifySSL();
|
|
|
+ // 设置协议http和https对应的处理socket链接工厂的对象
|
|
|
+ Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
|
|
|
+ .register("http", PlainConnectionSocketFactory.INSTANCE)
|
|
|
+ .register("https", new SSLConnectionSocketFactory(sslcontext))
|
|
|
+ .build();
|
|
|
+ manager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
|
|
|
+ manager.setMaxTotal(800);// 连接池最大并发连接数
|
|
|
+ manager.setDefaultMaxPerRoute(400);// 单路由最大并发数
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 绕过验证
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ * @throws NoSuchAlgorithmException
|
|
|
+ * @throws KeyManagementException
|
|
|
+ */
|
|
|
+ public static SSLContext createIgnoreVerifySSL() {
|
|
|
+ SSLContext sc = null;
|
|
|
+ try {
|
|
|
+ sc = SSLContext.getInstance("TLS");
|
|
|
+ // 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
|
|
|
+ X509TrustManager trustManager = new X509TrustManager() {
|
|
|
+ @Override
|
|
|
+ public void checkClientTrusted(
|
|
|
+ java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
|
|
|
+ String paramString) throws CertificateException {
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void checkServerTrusted(
|
|
|
+ java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
|
|
|
+ String paramString) throws CertificateException {
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public java.security.cert.X509Certificate[] getAcceptedIssuers() {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ sc.init(null, new TrustManager[]{trustManager}, null);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(ExceptionUtils.getFullStackTrace(e));
|
|
|
+ }
|
|
|
+ return sc;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 向指定 URL 发送GET方法的请求
|
|
|
*
|
|
@@ -232,6 +313,104 @@ public class HttpUtils
|
|
|
return result.toString();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * HTTP POST 请求
|
|
|
+ *
|
|
|
+ * @param url 请求url
|
|
|
+ * @param headers 请求头
|
|
|
+ * @param requestBodyString 表单字符串
|
|
|
+ * @param charset 编码
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String post(String url, Map<String, String> headers, String requestBodyString, String charset) throws IOException {
|
|
|
+ if (charset == null || charset.trim().isEmpty()) {
|
|
|
+ charset = CHARSET;
|
|
|
+ }
|
|
|
+ CloseableHttpClient httpClient = getHttpclient();
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+ HttpEntity entity = null;
|
|
|
+ try {
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ handleHeader(headers).forEach(header -> httpPost.addHeader(header));
|
|
|
+ StringEntity reqBodyParams = new StringEntity(requestBodyString, charset);
|
|
|
+ reqBodyParams.setContentType("application/json");
|
|
|
+ httpPost.setEntity(reqBodyParams);
|
|
|
+ response = httpClient.execute(httpPost);
|
|
|
+ entity = response.getEntity();
|
|
|
+ if (response.getStatusLine().getStatusCode() == 200) {
|
|
|
+ return EntityUtils.toString(entity, charset);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("post请求异常,请求地址:" + url + ", 异常信息:" + JSON.toJSONString(e));
|
|
|
+ throw e;
|
|
|
+ } finally {
|
|
|
+ closeALL(entity, response, httpClient);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static CloseableHttpClient getHttpclient() {
|
|
|
+ CloseableHttpClient httpclient = HttpClients.custom()
|
|
|
+ .setDefaultRequestConfig(requestConfig)
|
|
|
+ .setConnectionManager(manager)
|
|
|
+ .setRetryHandler(new DefaultHttpRequestRetryHandler())
|
|
|
+ .setConnectionManagerShared(true)
|
|
|
+ .build();
|
|
|
+ return httpclient;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理请求头
|
|
|
+ * Header只支持ASCII,如果使用中文需要编码
|
|
|
+ *
|
|
|
+ * @param headers map请求头
|
|
|
+ * @return header集合
|
|
|
+ */
|
|
|
+ private static List<Header> handleHeader(Map<String, String> headers) {
|
|
|
+ List<Header> headerArray = new ArrayList<>();
|
|
|
+ if (headers != null && !headers.isEmpty()) {
|
|
|
+ for (Map.Entry<String, String> entry : headers.entrySet()) {
|
|
|
+ Header header = new BasicHeader(entry.getKey(), entry.getValue());
|
|
|
+ headerArray.add(header);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Header header = new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36");
|
|
|
+ headerArray.add(header);
|
|
|
+ }
|
|
|
+ return headerArray;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 关闭所有连接
|
|
|
+ *
|
|
|
+ * @param entity
|
|
|
+ * @param response
|
|
|
+ * @param httpclient
|
|
|
+ */
|
|
|
+ private static void closeALL(HttpEntity entity, CloseableHttpResponse response, CloseableHttpClient httpclient) {
|
|
|
+ if (entity != null) {
|
|
|
+ try {
|
|
|
+ EntityUtils.consume(entity);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (response != null) {
|
|
|
+ try {
|
|
|
+ response.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (httpclient != null) {
|
|
|
+ try {
|
|
|
+ httpclient.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
private static class TrustAnyTrustManager implements X509TrustManager
|
|
|
{
|
|
|
@Override
|