package com.example.demo.util;
import org.apache.http.client.config.RequestConfig;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;
import java.io.IOException;
/**
* @author lyd
* @Description:
* @date 17:21
*/
@Component
public class HttpClientUtils {
private PoolingHttpClientConnectionManager poolingHttpClientConnectionManager;
public HttpClientUtils() {
this.poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
// 设置最大连接数
poolingHttpClientConnectionManager.setMaxTotal(200);
// 设置每个主机的并发数
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(20);
}
/**
* 设置请求信息
*
* @return
*/
private RequestConfig getConfig() {
// 配置信息
RequestConfig requestConfig = RequestConfig.custom()
// 设置连接超时时间
.setConnectTimeout(5000)
// 设置请求超时时间
.setConnectionRequestTimeout(5000)
// 设置读写超时时间
.setSocketTimeout(5000)
// 设置是否允许重定向
.setRedirectsEnabled(true).build();
return requestConfig;
}
/**
* get请求
*
* @param url
* @return
*/
public String doGet(String url) {
// 获取HttpClient对象
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(poolingHttpClientConnectionManager).build();
// 创建爱你httpGet请求,设置url地址
HttpGet httpGet = new HttpGet(url);
// 设置请求信息
httpGet.setConfig(getConfig());
// 使用HttpClient发起请求,获取响应
CloseableHttpResponse closeableHttpResponse = null;
try {
closeableHttpResponse = httpClient.execute(httpGet);
// 解析响应,返回结果
if (closeableHttpResponse.getStatusLine().getStatusCode() == 200) {
// 判断响应体是否不为空
if (closeableHttpResponse.getEntity() != null) {
String content = EntityUtils.toString(closeableHttpResponse.getEntity(), "UTF-8");
return content;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (closeableHttpResponse != null) {
// 关闭连接
closeableHttpResponse.close();
}
// 不能关闭,现在使用的是连接管理器
// httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* post请求
*
* @param url
* @return
*/
public String doPost(String url) {
// 获取HttpClient对象
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(poolingHttpClientConnectionManager).build();
// 创建爱你HttpPost请求,设置url地址
HttpPost httpPost = new HttpPost(url);
// 设置请求信息
httpPost.setConfig(getConfig());
// 使用HttpClient发起请求,获取响应
CloseableHttpResponse closeableHttpResponse = null;
try {
closeableHttpResponse = httpClient.execute(httpPost);
// 解析响应,返回结果
if (closeableHttpResponse.getStatusLine().getStatusCode() == 200) {
// 判断响应体是否不为空
if (closeableHttpResponse.getEntity() != null) {
String content = EntityUtils.toString(closeableHttpResponse.getEntity(), "UTF-8");
return content;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (closeableHttpResponse != null) {
// 关闭连接
closeableHttpResponse.close();
}
// 不能关闭,现在使用的是连接管理器
// httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}
HttpClient封装工具类