封装apache http
代码如下:
package com.mmkt.publicity.util;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
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.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class HttpUtil {
private static Logger log= LoggerFactory.getLogger(HttpUtil.class);
public static String sendGet(String url) {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
String result = "";
try {
httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
RequestConfig requestConfig = getRequestConfig();
httpGet.setConfig(requestConfig);
response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity);
} catch (Exception e) {
e.printStackTrace();
log.error("【远程调用接口异常!!!】");
}
finally {
closeResource(response, httpClient);
}
return result;
}
public static String sendformPost(List<NameValuePair> params, String url) {
CloseableHttpResponse response = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost( url);
String json = null;
try {
httpPost.addHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");
RequestConfig requestConfig = getRequestConfig();
httpPost.setConfig(requestConfig);
httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
Date startTime = new Date();
response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
JSONObject jsonObject = JSON.parseObject(EntityUtils.toString(entity, "UTF-8"));
json = jsonObject.getString("data");
Date endTime = new Date();
log.info("【时间戳】:{}-{} , 【时间差】:{}毫秒", startTime.getTime(), endTime.getTime(), endTime.getTime() - startTime.getTime());
} catch (Exception e) {
e.printStackTrace();
log.error("【远程调用接口异常!!!】");
}finally {
closeResource(response, httpClient);
}
return json;
}
public static String sendBodyPost(String url, JSONObject paramMap) {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
HttpPost post = new HttpPost(url);
String result = "";
try (CloseableHttpClient closeableHttpClient = httpClientBuilder.build()) {
HttpEntity entity = new StringEntity(paramMap.toString(), "UTF-8");
post.setEntity(entity);
post.setHeader("Content-type", "application/json");
HttpResponse resp = closeableHttpClient.execute(post);
try {
InputStream respIs = resp.getEntity().getContent();
byte[] respBytes = IOUtils.toByteArray(respIs);
result = new String(respBytes, Charset.forName("UTF-8"));
} catch (Exception e) {
e.printStackTrace();
}
return result;
} catch (IOException e) {
e.printStackTrace();
log.error("【远程调用接口异常!!!】");
}
return result;
}
public static List<NameValuePair> getNameValuePairList(Map<String, Object> map) {
List<NameValuePair> nameValuePairList = new ArrayList<>();
if (ObjectUtil.isNotEmpty(map)) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
nameValuePairList.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
}
return nameValuePairList;
}
private static void closeResource(CloseableHttpResponse response, CloseableHttpClient httpClient) {
if (null != response) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != httpClient) {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static RequestConfig getRequestConfig() {
return RequestConfig.custom()
.setSocketTimeout(5000)
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000).build();
}
}