import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
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.net.URLDecoder;
import java.util.List;
import java.util.Map;
public class HttpClientUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientUtil.class);
private static CloseableHttpClient client = HttpClients.custom().build();
/**
* 发送get请求
* @param url
* @return
* @throws IOException
*/
public static JSONObject doGetJson(String url,Map<String, String> headerMap,Map<String, Object> paramMap) {
LOGGER.info("HttpClientUtil发送get请求输入参数:url={},headerMap={},paramMap={}",url,headerMap,paramMap);
JSONObject jsonObject = new JSONObject();
HttpGet httpGet = null;
try {
HttpClient client = HttpClientBuilder.create().build();
if(paramMap!=null){
url = url+"?";
for(Map.Entry<String,Object> param : paramMap.entrySet()){
url = url + param.getKey()+"="+param.getValue()+"&";
}
url = url.substring(0,url.length()-1);
}
url = URLDecoder.decode(url, "UTF-8");
httpGet = new HttpGet(url);
if(headerMap!=null){
for(Map.Entry<String,String> header : headerMap.entrySet()){
httpGet.setHeader(header.getKey(),header.getValue());
}
}
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
String result = EntityUtils.toString(entity, "UTF-8");
jsonObject = JSON.parseObject(result);
}
LOGGER.info("HttpClientUtil发送发送get请求返回结果:{},参数:url={},headerMap={},paramMap={}",jsonObject.toJSONString(),url,headerMap,paramMap);
return jsonObject;
}catch (Exception e){
LOGGER.info("HttpClientUtil发送发送get请求执行异常,参数:url={},headerMap={},paramMap={},异常:{}",url,headerMap,paramMap,e);
return jsonObject;
}finally {
if(httpGet!=null){
httpGet.releaseConnection();
}
}
}
public static JSONObject doGetJson(String url) {
return doGetJson(url,null,null);
}
/**
* 无请求头参数传递json参数发送post请求
* @param url 请求连接
* @param paramMap 请求参数
* @return
*/
public static String doPostJsonWithoutHeader(String url, Map<String, Object> paramMap){
return doPost(url,paramMap,null,true);
}
/**
* 无请求头参数传递表单参数发送post请求
* @param url 请求连接
* @param paramMap 请求参数
* @return
*/
public static String doPostFormdataWithoutHeader(String url, Map<String, Object> paramMap){
return doPost(url,paramMap,null,false);
}
/**
* 传递json参数发送post请求
* @param url 请求连接
* @param paramMap 请求参数
* @param headerMap 请求参数
* @return
*/
public static String doPostJson(String url, Map<String, Object> paramMap,Map<String, String> headerMap){
return doPost(url,paramMap,headerMap,true);
}
/**
* 传递json参数发送post请求
*
* @param url 请求连接
* @param paramMap 请求参数
*/
public static String doPostJson(String url, Map<String, Object> paramMap) {
return doPost(url, paramMap, null, true);
}
/**
* 传递表单参数发送post请求
* @param url 请求连接
* @param paramMap 请求参数
* @param headerMap 请求头参数
* @return
*/
public static String doPostFormdata(String url, Map<String, Object> paramMap,Map<String, String> headerMap){
return doPost(url,paramMap,headerMap,false);
}
/**
* 发送post请求
* @param url 请求连接
* @param paramMap 请求参数
* @param headerMap 请求头参数
* @param isJson 是否传递json参数
* @return
*/
public static String doPost(String url, Map<String, Object> paramMap,Map<String, String> headerMap,boolean isJson){
LOGGER.info("HttpClientUtil发送post请求输入参数:url={},paramMap={},headerMap={},isJson={}",url,paramMap,headerMap,isJson);
HttpPost post = null;
try {
HttpClientBuilder builder = HttpClients.custom();
CloseableHttpClient client = builder.build();
post = new HttpPost(url);
if(isJson){
if(paramMap!=null){
String body = JSON.toJSONString(paramMap);
post.setEntity(new StringEntity(body,"utf-8"));
}
post.setHeader("Content-Type","application/json;charset=utf-8");
}else {
if(paramMap!=null){
List<NameValuePair> nameValuePairList = Lists.newArrayListWithCapacity(paramMap.size());
for(Map.Entry<String,Object> param : paramMap.entrySet()){
nameValuePairList.add(new BasicNameValuePair(param.getKey(),String.valueOf(param.getValue())));
}
StringEntity entity = new UrlEncodedFormEntity(nameValuePairList, "UTF-8");
post.setEntity(entity);
}
post.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
}
if(headerMap!=null){
for(Map.Entry<String,String> param : headerMap.entrySet()){
post.setHeader(param.getKey(),param.getValue());
}
}
HttpResponse resp = client.execute(post);
int code = resp.getStatusLine().getStatusCode();
String responseStr = EntityUtils.toString(resp.getEntity(), "utf-8");
LOGGER.info("HttpClientUtil发送post请求返回结果:{},参数:url={},paramMap={},headerMap={},isJson={}",responseStr,url,paramMap,headerMap,isJson);
if (code == 200) {
return responseStr;
}
return null;
}catch (Exception e){
LOGGER.info("HttpClientUtil发送post请求执行异常,参数:url={},paramMap={},headerMap={},isJson={},异常:{}",url,paramMap,headerMap,isJson,e);
return null;
}finally {
if(post!=null){
post.releaseConnection();
}
}
}
/**
* 发送post请求
* @param url 请求连接
* @param param 请求参数
* @param headerMap 请求头参数
* @param isJson 是否传递json参数
* @return
*/
public static String doPostString(String url, String param,Map<String, String> headerMap,boolean isJson){
LOGGER.info("HttpClientUtil发送post请求输入参数:url={},paramMap={},headerMap={},isJson={}",url,param,headerMap,isJson);
HttpPost post = null;
try {
post = new HttpPost(url);
if(isJson){
if(param!=null){
post.setEntity(new StringEntity(param, ContentType.APPLICATION_JSON));
}
post.setHeader("Content-Type","application/json;charset=utf-8");
}
HttpResponse resp = client.execute(post);
int code = resp.getStatusLine().getStatusCode();
String responseStr = EntityUtils.toString(resp.getEntity(), "utf-8");
LOGGER.info("HttpClientUtil发送post请求返回结果:{},参数:url={},paramMap={},headerMap={},isJson={}",responseStr,url,param,headerMap,isJson);
if (code == 200) {
return responseStr;
}
return null;
}catch (Exception e){
LOGGER.info("HttpClientUtil发送post请求执行异常,参数:url={},paramMap={},headerMap={},isJson={},异常:{}",url,param,headerMap,isJson,e);
return null;
}finally {
if(post!=null){
post.releaseConnection();
}
}
}
/**
* 发送post请求
* @param url 请求连接
* @param param 请求参数
* @param isJson 是否传递json参数
* @return
*/
public static String doPostStringV2(String url, String param,boolean isJson){
LOGGER.info("HttpClientUtil发送post请求输入参数:url={},paramMap={},isJson={}",url,param,isJson);
HttpPost post = null;
try {
post = new HttpPost(url);
if(isJson){
if(param!=null){
// String body = JSON.toJSONString(paramMap);
post.setEntity(new StringEntity(param, ContentType.APPLICATION_JSON));
}
post.setHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
}
HttpResponse resp = client.execute(post);
int code = resp.getStatusLine().getStatusCode();
String responseStr = EntityUtils.toString(resp.getEntity(), "utf-8");
LOGGER.info("HttpClientUtil发送post请求返回结果:{},参数:url={},paramMap={},isJson={}",responseStr,url,param,isJson);
if (code == 200) {
return responseStr;
}
return null;
}catch (Exception e){
LOGGER.info("HttpClientUtil发送post请求执行异常,参数:url={},paramMap={},isJson={},异常:{}",url,param,isJson,e);
return null;
}finally {
if(post!=null){
post.releaseConnection();
}
}
}
}
|