```java
package com.chencongping.http;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
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.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.util.ObjectUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class HttpClientUtils {
public static String SUCCESS = "000000";
public static String FAIL = "111111";
private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(360000).setConnectTimeout(360000).build();
public static JSONObject httpPost(String url, Map<String, Object> map, Map<String, String> headerMap) {
url = url.trim();
HttpPost httpPost = null;
JSONObject jsonResult = null;
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
httpPost = new HttpPost(url);
setHeader(headerMap, httpPost);
httpPost.setConfig(requestConfig);
if (null != map) {
StringEntity entity = new StringEntity(JSON.toJSONString(map), "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
}
CloseableHttpResponse result = httpClient.execute(httpPost);
if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String str = "";
try {
str = EntityUtils.toString(result.getEntity(), "utf-8");
log.info(str);
jsonResult = JSONObject.parseObject(str);
} catch (Exception e) {
log.error("post请求提交失败:" + url, e.getMessage());
}
} else {
log.error("请求接口" + url + ",状态码:" + result.getStatusLine().getStatusCode());
}
} catch (IOException e) {
log.error("post请求提交失败:" + url, e.getMessage());
throw new RuntimeException(e);
} finally {
if (httpPost != null) {
httpPost.releaseConnection();
}
}
return jsonResult;
}
private static void setHeader(Map<String, String> headerMap, HttpPost httpPost) {
if (!ObjectUtils.isEmpty(headerMap)) {
Iterator<Entry<String, String>> iterator = headerMap.entrySet().iterator();
while (iterator.hasNext()) {
Entry<String, String> entry = iterator.next();
String headerKey = entry.getKey();
String headerValue = entry.getValue();
httpPost.addHeader(headerKey, headerValue);
}
}
}
public static JSONObject httpGet(String url) {
JSONObject jsonResult = null;
CloseableHttpClient client = HttpClients.createDefault();
HttpGet request = new HttpGet(url);
request.setConfig(requestConfig);
try {
CloseableHttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
String strResult = EntityUtils.toString(entity, "utf-8");
jsonResult = JSONObject.parseObject(strResult);
} else {
log.error("get请求提交失败:" + url);
}
} catch (IOException e) {
log.error("get请求提交失败:" + url, e);
} finally {
request.releaseConnection();
}
return jsonResult;
}
}