Java封装httpclient的get和post请求
直接上代码,复制粘贴后,需要导入依赖则导入对应依赖,传入url地址和map类型的参数直接调用公共方法即可。
package com.yonyoufutong.utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.config.CookieSpecs;
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.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.util.EntityUtils;
import java.io.IOException;
import java.util.Map;
@Slf4j
public class RequestTools {
private static final String HEADER_CONTENT_JSON = "application/json";
private static final String DEFAULT_CHARSET = "UTF-8";
private static PoolingHttpClientConnectionManager cm = null;
private static CloseableHttpClient httpClient;
private static boolean islog = false;
static {
cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(500);
cm.setDefaultMaxPerRoute(50);
RequestConfig globalConfig = RequestConfig.custom()
.setConnectionRequestTimeout(5000) // 连接池获取连接超时
.setConnectTimeout(5000) // 连接建立超时
.setSocketTimeout(20000) // 等待响应超时
.setCookieSpec(CookieSpecs.IGNORE_COOKIES)
.build();
httpClient = HttpClients.custom().setConnectionManager(cm).setDefaultRequestConfig(globalConfig).build();
}
private static CloseableHttpClient getHttpClient() {
return httpClient;
}
/**
* @param requestUrl
* @param paramMap
* @return
* @throws IOException
*/
public static String doGet(String requestUrl, Map<String, Object> paramMap) throws IOException {
CloseableHttpClient httpClient = getHttpClient();
StringBuilder param = new StringBuilder("?");
if (paramMap != null) {
for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
param.append(entry.getKey());
param.append("=");
param.append(entry.getValue());
param.append("&");
}
param.deleteCharAt(param.length() - 1);
}
if (islog) {
System.out.println("这是请求参数:" + param);
}
String url = requestUrl + param;
HttpGet get = new HttpGet(url);
String responseString = httpClient.execute(get, response -> EntityUtils.toString(response.getEntity()));
if (islog) {
System.out.println("这是返回数据:" + responseString);
}
get.releaseConnection();
return responseString;
}
public static String doPost(String requestUrl, Object param) throws IOException {
CloseableHttpClient httpClient = getHttpClient();
HttpPost post = new HttpPost(requestUrl);
post.setHeader("Content-Type", "application/json;charset=UTF-8");
ObjectMapper mapper = new ObjectMapper();
String requestParam = mapper.writeValueAsString(param);
if (islog) {
System.out.println("这是请求参数:" + requestParam);
}
// log.error("请求参数:" + requestParam);
post.setEntity(new StringEntity(requestParam, "text/plain", "UTF-8"));
String responseString = httpClient.execute(post, response -> EntityUtils.toString(response.getEntity()));
if (islog) {
System.out.println("这是返回数据:" + responseString);
}
// log.error("返回参数:" + responseString);
return responseString;
}
}