@Slf4j
public class DecHttpClintUtils {
private final static int SocketTimeout = 1 * 60 * 6 * 1000;
private final static int ConnectTimeout = 1 * 5 * 1000;
private final static boolean Redirects = false;
private final static String Charset = "utf-8";
private static RequestConfig requestConfig = null;
static {
// 设置请求和传输超时时间
requestConfig = RequestConfig
.custom()
.setSocketTimeout(SocketTimeout)
.setConnectTimeout(ConnectTimeout)
.setRedirectsEnabled(Redirects)
.build();
}
/**
* post请求传输json参数
*
* @param url url地址
* @param jsonParam 参数
* @return
*/
public static JSONObject doPost(URI url, JSONObject jsonParam, Map<String, String> head) {
// post请求返回结果
CloseableHttpClient httpClient = HttpClients.createDefault();
JSONObject jsonResult = null;
CloseableHttpResponse result =null;
HttpPost httpPost = new HttpPost(url);
// 设置请求和传输超时时间
httpPost.setConfig(requestConfig);
try {
if (!CollectionUtils.isEmpty(head)) {
for (Map.Entry<String, String> entry : head.entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
}
if (null != jsonParam) {
// 解决中文乱码问题
StringEntity entity = new StringEntity(jsonParam.toString(), Charset);
entity.setContentEncoding(Charset);
entity.setContentType("application/json");
httpPost.setEntity(entity);
}
result = httpClient.execute(httpPost);
// 请求发送成功,并得到响应
log.error("状态码{}", result.getStatusLine().getStatusCode() + "");
if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String str = "";
try {
// 读取服务器返回过来的json字符串数据
str = EntityUtils.toString(result.getEntity(), Charset);
// 把json字符串转换成json对象
jsonResult = JSONObject.parseObject(str);
} catch (Exception e) {
log.error("post请求提交失败:" + url, e);
}
}
} catch (IOException e) {
log.error("post请求提交失败:" + url, e);
} finally {
closeResources(httpClient,result);
}
return jsonResult;
}
/**
* post请求传输String参数 例如:name=Jack&sex=1&type=2
* Content-type:application/x-www-form-urlencoded
*
* @param url url地址
* @param strParam 参数
* @return
*/
public static JSONObject doPost(URI url, String strParam, Map<String, String> head) {
// post请求返回结果
CloseableHttpClient httpClient = HttpClients.createDefault();
JSONObject jsonResult = null;
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
CloseableHttpResponse result = null;
try {
if (!CollectionUtils.isEmpty(head)) {
for (Map.Entry<String, String> entry : head.entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
}
if (null != strParam) {
// 解决中文乱码问题
StringEntity entity = new StringEntity(strParam, Charset);
entity.setContentEncoding(Charset);
entity.setContentType("application/x-www-form-urlencoded");
httpPost.setEntity(entity);
}
result = httpClient.execute(httpPost);
// 请求发送成功,并得到响应
if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String str = "";
try {
// 读取服务器返回过来的json字符串数据
str = EntityUtils.toString(result.getEntity(), Charset);
// 把json字符串转换成json对象
jsonResult = JSONObject.parseObject(str);
} catch (Exception e) {
log.error("post请求提交失败:" + url, e);
}
}
} catch (IOException e) {
log.error("post请求提交失败:" + url, e);
} finally {
closeResources(httpClient,result);
}
return jsonResult;
}
/**
* 发送get请求
*
* @param url 路径
* @return
*/
public static JSONObject doGet(URI url, Map<String, String> head) {
// get请求返回结果
JSONObject jsonResult = null;
CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = null;
// 发送get请求
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(requestConfig);
try {
if (!CollectionUtils.isEmpty(head)) {
for (Map.Entry<String, String> entry : head.entrySet()) {
httpGet.setHeader(entry.getKey(), entry.getValue());
}
}
response = client.execute(httpGet);
// 请求发送成功,并得到响应
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 读取服务器返回过来的json字符串数据
HttpEntity entity = response.getEntity();
String strResult = EntityUtils.toString(entity, Charset);
// 把json字符串转换成json对象
jsonResult = JSONObject.parseObject(strResult);
} else {
log.error("get请求提交失败:" + url);
}
} catch (IOException e) {
log.error("get请求提交失败:" + url, e);
} finally {
closeResources(client,response);
}
return jsonResult;
}
public static JSONObject doGet(URI url, Map<String, String> param, Map<String, String> head) {
// get请求返回结果
JSONObject jsonResult = null;
CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = null;
HttpGet httpGet = null;
try {
URIBuilder builder = new URIBuilder(url);
if (param != null) {
for (Map.Entry<String, String> entry : param.entrySet()) {
builder.addParameter(entry.getKey(), entry.getValue());
}
}
URI uri = builder.build();
// 发送get请求
httpGet = new HttpGet(uri);
httpGet.setConfig(requestConfig);
if (!CollectionUtils.isEmpty(head)) {
for (Map.Entry<String, String> entry : head.entrySet()) {
httpGet.setHeader(entry.getKey(), entry.getValue());
}
}
response = client.execute(httpGet);
// 请求发送成功,并得到响应
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 读取服务器返回过来的json字符串数据
HttpEntity entity = response.getEntity();
String strResult = EntityUtils.toString(entity, Charset);
// 把json字符串转换成json对象
jsonResult = JSONObject.parseObject(strResult);
} else {
log.error("get请求提交失败:" + url);
}
} catch (IOException e) {
log.error("get请求提交失败:" + url, e);
} catch (URISyntaxException e) {
log.error("get请求提交失败:" + url, e);
} finally {
closeResources(client,response);
}
return jsonResult;
}
/**
* 关闭资源
*
* @param httpClient
* @param response
*/
private static void closeResources(CloseableHttpClient httpClient, CloseableHttpResponse response) {
try {
if (response != null) {
response.close();
}
if (httpClient != null) {
httpClient.close();
}
} catch (IOException e) {
log.error("HttpClintUtils关闭连接失败:{}", e);
}
}
}