public static String sendGetByParam(String url, final List<Header> headerList, final List<NameValuePair> paramList,
final int reSend) {
//声明返回结果
String result = "";
//开始请求API接口时间
TimeInterval timer = DateUtil.timer();
//请求API接口的响应时间
long endTime = 0L;
HttpEntity httpEntity = null;
CloseableHttpResponse httpResponse = null;
CloseableHttpClient httpClient = null;
try {
// 创建连接
httpClient = HttpClientFactory.getInstance().getHttpClient();
//创建URIBuilder
URIBuilder uriBuilder = new URIBuilder(url);
if (CollUtil.isNotEmpty(paramList)) {
for (NameValuePair nm : paramList) {
//设置参数
uriBuilder.setParameter(nm.getName(), nm.getValue());
}
}
url = uriBuilder.build().toString();
LOG.info("请求URL;{}", url);
// 设置请求头和报文
final HttpGet httpGet = HttpClientFactory.getInstance().httpGet(url);
if (CollUtil.isNotEmpty(headerList)) {
httpGet.setHeaders(headerList.toArray(new Header[0]));
}
httpGet.setHeader("User-Agent",
"Mozilla/5.0(Windows NT 6.1;Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
//
// !!!!!!!!!! 这里加上了no-store 禁止缓存了!!!!!!!!!
//
httpGet.setHeader("cache-control", "no-store, must-revalidate");
//执行发送,获取相应结果
httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);
} catch (final Exception e) {
LOG.error("请求:{}接口出现异常", url, e);
if (reSend > 0) {
LOG.info("请求:{}出现异常:{},进行重发。进行第:{}次重发", url, e.getMessage(), (HttpConstant.REQ_TIMES - reSend + 1));
result = sendPostByParam(url, headerList, paramList, reSend - 1);
if (result != null && !"".equals(result)) {
return result;
}
}
} finally {
try {
EntityUtils.consume(httpEntity);
} catch (final IOException e) {
LOG.error("http请求释放资源异常", e);
}
}
LOG.info("spendtime:{},url:{},result:{}", timer.interval(), url, result);
return result;
}
```java
package com.redu.taotie.utils.http;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
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.LayeredConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import javax.net.ssl.SSLContext;
import java.security.NoSuchAlgorithmException;
/**
@author : harara
@version : 2.0
@date : 2019/9/27 10:12
/
public class HttpClientFactory {
private static HttpClientFactory instance = null;
private HttpClientFactory() {
}
public synchronized static HttpClientFactory getInstance() {
if (instance == null) {
instance = new HttpClientFactory();
}
return instance;
}
public synchronized CloseableHttpClient getHttpClient() {
CloseableHttpClient httpClient = null;
if (HttpConstant.IS_KEEP_ALIVE) {
//获取长连接
httpClient = new KeepAliveHttpClientBuilder().getKeepAliveHttpClient();
} else {
// 获取短连接
httpClient = new HttpClientBuilder().getHttpClient();
}
return httpClient;
}
public HttpPost httpPost(String httpUrl) {
HttpPost httpPost = null;
httpPost = new HttpPost(httpUrl);
if (HttpConstant.IS_KEEP_ALIVE) {
// 设置为长连接,服务端判断有此参数就不关闭连接。
httpPost.setHeader("Connection", "Keep-Alive");
}
return httpPost;
}
public HttpGet httpGet(String httpUrl) {
HttpGet httpGet = new HttpGet(httpUrl);
if (HttpConstant.IS_KEEP_ALIVE) {
// 设置为长连接,服务端判断有此参数就不关闭连接。
httpGet.setHeader("Connection", "Keep-Alive");
}
return httpGet;
}
private static class KeepAliveHttpClientBuilder {
private static CloseableHttpClient httpClient;
/**
* 使用连接池
*/
private synchronized CloseableHttpClient getKeepAliveHttpClient() {
if (httpClient == null) {
LayeredConnectionSocketFactory sslsf = null;
try {
sslsf = new SSLConnectionSocketFactory(SSLContext.getDefault());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
Registry<ConnectionSocketFactory> socketFactoryRegistry =
RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslsf)
.register("http", new PlainConnectionSocketFactory()).build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
cm.setMaxTotal(HttpConstant.MAX_TOTAL);
cm.setDefaultMaxPerRoute(HttpConstant.MAX_CONN_PER_ROUTE);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(HttpConstant.CONNECT_TIMEOUT)
.setSocketTimeout(HttpConstant.SOCKET_TIMEOUT).build();
// 创建连接
httpClient = HttpClients.custom().disableAuthCaching().setDefaultRequestConfig(requestConfig)
.setConnectionManager(cm).build();
}
return httpClient;
}
}
private static class HttpClientBuilder {
private CloseableHttpClient httpClient;
/**
* 获取http短连接
*/
private synchronized CloseableHttpClient getHttpClient() {
if (httpClient == null) {
/**
* host = http-dyn.abuyun.com
* port = 9020
* user = HC0YY80N2T8N8TVD
* password = E8EF34CEAEAEF093
*/
// String username = "HC0YY80N2T8N8TVD";
// String password = "E8EF34CEAEAEF093";
// int port = 9020;
// String host = "http-dyn.abuyun.com";
// //设置代理地址、代理端口号、协议类型
// HttpHost proxy = new HttpHost(host, port, "http");
// DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
// //创建认证,并设置认证范围
// CredentialsProvider credsProvider = new BasicCredentialsProvider();
// credsProvider.setCredentials(new AuthScope(host, port),
// new UsernamePasswordCredentials(username, password));
RequestConfig requestConfig = RequestConfig.custom()
// 设置请求超时时间
.setConnectTimeout(HttpConstant.CONNECT_TIMEOUT)
// 设置响应超时时间
.setSocketTimeout(HttpConstant.SOCKET_TIMEOUT).build();
// 创建连接
httpClient = HttpClients.custom().disableAuthCaching().setDefaultRequestConfig(requestConfig).build();
// httpClient =
// HttpClients.custom().setRoutePlanner(routePlanner).setDefaultCredentialsProvider(credsProvider)
// .disableAuthCaching().setDefaultRequestConfig(requestConfig).build();
// int port = 9020;
// String host = "http-dyn.abuyun.com";
// //设置代理地址、代理端口号、协议类型
// HttpHost proxy = new HttpHost(host, port, "http");
// DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
}
return httpClient;
}
}
}
```