import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.Map;
@Slf4j
public class HttpUtils {
public static String doGet(String url, Map<String, String> requestMap, Map<String, String> headMap) throws IOException {
if (requestMap != null && requestMap.size() > 0) {
url += "?";
StringBuilder sb = new StringBuilder(url);
for (String key : requestMap.keySet()) {
sb.append(key).append("=").append(requestMap.get(key)).append("&");
}
url = sb.toString();
url = url.substring(0, url.length() - 1);
}
log.info("GET请求,请求地址:[{}],请求头:[{}]", url, JSONObject.toJSONString(headMap));
HttpGet httpGet = new HttpGet(url);
return doHttpRequest(httpGet, headMap);
}
public static String doPost(String url, String str, Map<String, String> headMap) throws IOException {
log.info("POST请求,请求地址:[{}],请求参数:[{}],请求头:[{}]", url, str, JSONObject.toJSONString(headMap));
HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(str, "UTF-8");
httpPost.setEntity(entity);
return doHttpRequest(httpPost, headMap);
}
public static String doPut(String url, String str, Map<String, String> headMap) throws IOException {
log.info("PUT请求,请求地址:[{}],请求参数:[{}],请求头:[{}]", url, str, JSONObject.toJSONString(headMap));
HttpPut httpPut = new HttpPut(url);
StringEntity entity = new StringEntity(str, "UTF-8");
httpPut.setEntity(entity);
return doHttpRequest(httpPut, headMap);
}
public static String doDelete(String url, Map<String, String> requestMap, Map<String, String> headMap) throws IOException {
if (requestMap != null && requestMap.size() > 0) {
url += "?";
StringBuilder sb = new StringBuilder(url);
for (String key : requestMap.keySet()) {
sb.append(key).append("=").append(requestMap.get(key)).append("&");
}
url = sb.toString();
url = url.substring(0, url.length() - 1);
}
log.info("DELETE请求,请求地址:[{}],请求头:[{}]", url, JSONObject.toJSONString(headMap));
HttpDelete httpDelete = new HttpDelete(url);
return doHttpRequest(httpDelete, headMap);
}
private static String doHttpRequest(HttpRequestBase httpRequest, Map<String, String> headMap) throws IOException {
httpRequest.setHeader("Content-type", "application/json");
httpRequest.setHeader("DataEncoding", "UTF-8");
if (headMap != null) {
for (String headKey : headMap.keySet()) {
httpRequest.setHeader(headKey, headMap.get(headKey));
}
}
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(6000)
.setConnectionRequestTimeout(6000)
.setSocketTimeout(6000).build();
httpRequest.setConfig(requestConfig);
CloseableHttpResponse httpResponse = null;
// 设置使用系统代理
CloseableHttpClient httpClient = HttpClientBuilder.create().useSystemProperties().build();
try {
httpResponse = httpClient.execute(httpRequest);
HttpEntity entity = httpResponse.getEntity();
return EntityUtils.toString(entity, "UTF-8");
} finally {
if (httpResponse != null) {
try {
httpResponse.close();
} catch (IOException e) {
log.error("");
}
}
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
log.error("");
}
}
}
}
}