import org.apache.http.HttpEntity;
import org.apache.http.NamevaluePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.client.methods.HttpRequestbase;
import org.apache.http.client.utils.URIBuilder;
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.util.EntityUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class HttpClientUtil {
private static PoolingHttpClientConnectionManager connManager;
private static final Map EMPTY_MAP = new HashMap<>(2);
private static final String UTF_8 = "UTF-8";
private static void init() {
if (connManager == null) {
connManager = new PoolingHttpClientConnectionManager();
//设置连接池最大连接数
connManager.setMaxTotal(50);
//设置每隔路由最大连接数
connManager.setDefaultMaxPerRoute(5);
}
}
private static CloseableHttpClient getHttpClient() {
//初始化连接池
init();
return HttpClients.custom().setConnectionManager(connManager).build();
}
public static Map doGet(String url) {
HttpGet httpGet = new HttpGet(url);
return getResult(httpGet);
}
public static Map doGet(String url, Map params) throws URISyntaxException {
URIBuilder uriBuilder = new URIBuilder(url);
if (!params.isEmpty()) {
//设置请求参数
uriBuilder.setParameters(coverParams(params));
}
HttpGet httpGet = new HttpGet(uriBuilder.build());
return getResult(httpGet);
}
public static Map doGet(String url, Map headers, Map params) throws URISyntaxException {
URIBuilder uriBuilder = new URIBuilder(url);
if (!params.isEmpty()) {
//设置请求参数
uriBuilder.setParameters(coverParams(params));
}
HttpGet httpGet = new HttpGet(uriBuilder.build());
//遍历headers将请求头放入http请求头中
headers.entrySet().forEach(x -> httpGet.setHeader(x.getKey(), String.valueOf(x.getValue())));
return getResult(httpGet);
}
public static Map doPost(String url) {
return getResult(new HttpPost(url));
}
public static Map doPost(String url, String json) {
HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(json, UTF_8);
entity.setContentEncoding(UTF_8);
entity.setContentType("application/json");
httpPost.setEntity(entity);
return getResult(httpPost);
}
public static Map doPost(String url, Map params) throws UnsupportedEncodingException {
HttpPost httpPost = new HttpPost(url);
List nvps = coverParams(params);
httpPost.setEntity(new UrlEncodedFormEntity(nvps, UTF_8));
return getResult(httpPost);
}
public static Map doPost(String url, Map headers, Map params)
throws UnsupportedEncodingException {
HttpPost httpPost = new HttpPost(url);
headers.entrySet().forEach(x -> httpPost.addHeader(x.getKey(), String.valueOf(x.getValue())));
httpPost.setEntity(new UrlEncodedFormEntity(coverParams(params), "utf-8"));
return getResult(httpPost);
}
private static List coverParams(Map params) {
ArrayList namevaluePairs = new ArrayList<>();
params.entrySet().forEach(x -> namevaluePairs.add(new BasicNamevaluePair(x.getKey(), String.valueOf(x.getValue()))));
return namevaluePairs;
}
private static Map getResult(HttpRequestbase req) {
//返回值对象
HashMap resultMap = new HashMap<>();
//获取HttpClient对象
CloseableHttpClient httpClient = getHttpClient();
try {
//发送请求 获取响应对象
CloseableHttpResponse response = httpClient.execute(req);
HttpEntity entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
if (entity != null) {
//将响应实体转换为string类型
String result = EntityUtils.toString(entity);
//关闭响应对象
response.close();
resultMap.put("code", String.valueOf(statusCode));
resultMap.put("data", result);
return resultMap;
}
} catch (IOException e) {
e.printStackTrace();
}
return EMPTY_MAP;
}
}