packagecom.jt.common.service;importjava.util.ArrayList;importjava.util.List;importjava.util.Map;importorg.apache.http.NameValuePair;importorg.apache.http.client.config.RequestConfig;importorg.apache.http.client.entity.UrlEncodedFormEntity;importorg.apache.http.client.methods.CloseableHttpResponse;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.client.utils.URIBuilder;importorg.apache.http.entity.StringEntity;importorg.apache.http.impl.client.CloseableHttpClient;importorg.apache.http.message.BasicNameValuePair;importorg.apache.http.util.EntityUtils;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;
@Servicepublic classHttpClientService {private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientService.class);
@Autowired(required=false)privateCloseableHttpClient httpClient;
@Autowired(required=false)privateRequestConfig requestConfig;/*** 执行get请求
*
*@paramurl
*@return*@throwsException*/
public String doGet(String url,Map params,String encode) throwsException {
LOGGER.info("执行GET请求,URL = {}", url);if(null !=params){
URIBuilder builder= newURIBuilder(url);for (Map.Entryentry : params.entrySet()) {
builder.setParameter(entry.getKey(), entry.getValue());
}
url=builder.build().toString();
}//创建http GET请求
HttpGet httpGet = newHttpGet(url);
httpGet.setConfig(requestConfig);
CloseableHttpResponse response= null;try{//执行请求
response =httpClient.execute(httpGet);//判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {if(encode == null){
encode= "UTF-8";
}returnEntityUtils.toString(response.getEntity(), encode);
}
}finally{if (response != null) {
response.close();
}//此处不能关闭httpClient,如果关闭httpClient,连接池也会销毁
}return null;
}public String doGet(String url, String encode) throwsException{return this.doGet(url, null, encode);
}public String doGet(String url) throwsException{return this.doGet(url, null, null);
}/*** 带参数的get请求
*
*@paramurl
*@paramparams
*@return*@throwsException*/
public String doGet(String url, Map params) throwsException {return this.doGet(url, params, null);
}/*** 执行POST请求
*
*@paramurl
*@paramparams
*@return*@throwsException*/
public String doPost(String url, Map params,String encode) throwsException {//创建http POST请求
HttpPost httpPost = newHttpPost(url);
httpPost.setConfig(requestConfig);if (null !=params) {//设置2个post参数,一个是scope、一个是q
List parameters = new ArrayList(0);for (Map.Entryentry : params.entrySet()) {
parameters.add(newBasicNameValuePair(entry.getKey(), entry.getValue()));
}//构造一个form表单式的实体
UrlEncodedFormEntity formEntity = null;if(encode!=null){
formEntity= newUrlEncodedFormEntity(parameters,encode);
}else{
formEntity= newUrlEncodedFormEntity(parameters);
}//将请求实体设置到httpPost对象中
httpPost.setEntity(formEntity);
}
CloseableHttpResponse response= null;try{//执行请求
response =httpClient.execute(httpPost);//判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {return EntityUtils.toString(response.getEntity(), "UTF-8");
}
}finally{if (response != null) {
response.close();
}
}return null;
}/*** 执行POST请求
*
*@paramurl
*@paramparams
*@return*@throwsException*/
public String doPost(String url, Map params) throwsException {//创建http POST请求
HttpPost httpPost = newHttpPost(url);
httpPost.setConfig(requestConfig);if (null !=params) {//设置2个post参数,一个是scope、一个是q
List parameters = new ArrayList(0);for (Map.Entryentry : params.entrySet()) {
parameters.add(newBasicNameValuePair(entry.getKey(), entry.getValue()));
}//构造一个form表单式的实体
UrlEncodedFormEntity formEntity = newUrlEncodedFormEntity(parameters);//将请求实体设置到httpPost对象中
httpPost.setEntity(formEntity);
}
CloseableHttpResponse response= null;try{//执行请求
response =httpClient.execute(httpPost);//判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {return EntityUtils.toString(response.getEntity(), "UTF-8");
}
}finally{if (response != null) {
response.close();
}
}return null;
}public String doPostJson(String url, String json) throwsException {//创建http POST请求
HttpPost httpPost = newHttpPost(url);
httpPost.setConfig(requestConfig);if(null !=json){//设置请求体为 字符串
StringEntity stringEntity = new StringEntity(json,"UTF-8");
httpPost.setEntity(stringEntity);
}
CloseableHttpResponse response= null;try{//执行请求
response =httpClient.execute(httpPost);//判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {return EntityUtils.toString(response.getEntity(), "UTF-8");
}
}finally{if (response != null) {
response.close();
}
}return null;
}
}