一、pom
<?xml version="1.0" encoding="UTF-8"?>
<projectxmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.java.gaode.GaoDeDemo</groupId>
<artifactId>GaoDeDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.google.collections</groupId>
<artifactId>google-collections</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.9</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.28</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
</dependencies>
</project>
二、HttpClientUtils工具类
package com.java.gao;
/**
* @ClassName: HttpClientUtils
* @Description:
* @Version: v1.0.0
* @Author: Fu Hao
* @Date: 2019/10/22 0022 下午 8:15
* Modification History:
* Date Author Version Description
* -------------------------------------------------------------
* 2019/10/22 0022 Fu Hao v1.0.0 创建
*/
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Lists;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NoHttpResponseException;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.ConnectTimeoutException;
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.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.UnknownHostException;
import java.nio.charset.CodingErrorAction;
import java.util.List;
import java.util.Map;
/**
* HttpClient工具类
*/
public class HttpClientUtils {
/**
* 连接池最大连接数
*/
private static final int MAX_TOTAL_CONNECTIONS = 4000;
/**
* 设置每个路由上的默认连接个数
*/
private static final int DEFAULT_MAX_PER_ROUTE = 200;
/**
* 请求的请求超时时间 单位:毫秒
*/
private static final int REQUEST_CONNECTION_TIMEOUT = 8 * 1000;
/**
* 请求的等待数据超时时间 单位:毫秒
*/
private static final int REQUEST_SOCKET_TIMEOUT = 8 * 1000;
/**
* 请求的连接超时时间 单位:毫秒
*/
private static final int REQUEST_CONNECTION_REQUEST_TIMEOUT = 5 * 1000;
/**
* 连接闲置多久后需要重新检测 单位:毫秒
*/
private static final int VALIDATE_AFTER_IN_ACTIVITY = 2 * 1000;
/**
* 关闭Socket时,要么发送完所有数据,要么等待多少秒后,就关闭连接,此时socket.close()是阻塞的 单位秒
*/
private static final int SOCKET_CONFIG_SO_LINGER = 60;
/**
* 接收数据的等待超时时间,即读超时时间,单位ms
*/
private static final int SOCKET_CONFIG_SO_TIMEOUT = 5 * 1000;
/**
* 重试次数
*/
private static int RETRY_COUNT = 5;
/**
* 声明为 static volatile,会迫使线程每次读取时作为一个全局变量读取
*/
private static volatile CloseableHttpClient httpClient = null;
/**
* @param uri
* @return String
* @description get请求方式
* @author: long.he01
*/
public static String doGet(String uri) {
String responseBody;
HttpGet httpGet = new HttpGet(uri);
try {
httpGet.setConfig(getRequestConfig());
responseBody = executeRequest(httpGet);
} catch (IOException e) {
throw new RuntimeException("httpclient doGet方法异常 ", e);
} finally {
httpGet.releaseConnection();
}
return responseBody;
}
/**
* @param uri
* @param params
* @return string
* @description 带map参数get请求, 此方法会将map参数拼接到连接地址上。
*/
public static String doGet(String uri, Map<String, String> params) {
return doGet(getGetUrlFromParams(uri, params));
}
/**
* @param uri
* @param params
* @return String
*