package com.22pig.util;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
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.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @Description
* @author
* @date 2016年10月14日 下午4:04:11
*/
/**
*
*
* 分装一个http请求的工具类
*/
public class HttpClientUtil {
private static final Logger log = LoggerFactory
.getLogger(HttpClientUtil.class);
/**
*
*
* 初始化HttpClient
*/
private CloseableHttpClient httpClient = HttpClients.createDefault();
/**
*
*
* POST方式调用
*
*
*
*
*
* @param url
*
*
* @param params
* 参数为NameValuePair键值对对象
*
*
* @return 响应字符串
*
*
* @throws java.io.UnsupportedEncodingException
*/
public String executeByPOST(String url, List<NameValuePair> params) {
HttpPost post = new HttpPost(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseJson = null;
try {
if (params != null) {
post.setEntity(new UrlEncodedFormEntity(params));
}
responseJson = httpClient.execute(post, responseHandler);
log.info(“HttpClient POST请求结果:” + responseJson);
}
catch (ClientProtocolException e) {
e.printStackTrace();
log.info(“HttpClient POST请求异常:” + e.getMessage());
}
catch (IOException e) {
e.printStackTrace();
}
finally {
httpClient.getConnectionManager().closeExpiredConnections();
httpClient.getConnectionManager().closeIdleConnections(30,
TimeUnit.SECONDS);
}
return responseJson;
}
/**
*
*
* Get方式请求
*
*
*
*
*
* @param url
* 带参数占位符的URL,例:http://ip/User/user/center.aspx?_action=GetSimpleUserInfo
* &codes={0}&email={1}
*
*
* @param params
* 参数值数组,需要与url中占位符顺序对应
*
*
* @return 响应字符串
*
*
* @throws java.io.UnsupportedEncodingException
*/
public String executeByGET(String url, Object[] params) {
String messages = MessageFormat.format(url, params);
HttpGet get = new HttpGet(messages);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseJson = null;
try {
responseJson = httpClient.execute(get, responseHandler);
log.info(“HttpClient GET请求结果:” + responseJson);
}
catch (ClientProtocolException e) {
e.printStackTrace();
log.error(“HttpClient GET请求异常:” + e.getMessage());
}
catch (IOException e) {
e.printStackTrace();
log.error(“HttpClient GET请求异常:” + e.getMessage());
}
finally {
httpClient.getConnectionManager().closeExpiredConnections();
httpClient.getConnectionManager().closeIdleConnections(30,
TimeUnit.SECONDS);
}
return responseJson;
}
/**
*
*
* @param url
*
*
* @return
*/
public String executeByGET(String url) {
HttpGet get = new HttpGet(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseJson = null;
try {
responseJson = httpClient.execute(get, responseHandler);
log.info(“HttpClient GET请求结果:” + responseJson);
}
catch (ClientProtocolException e) {
e.printStackTrace();
log.error(“HttpClient GET请求异常:” + e.getMessage());
}
catch (IOException e) {
e.printStackTrace();
log.error(“HttpClient GET请求异常:” + e.getMessage());
}
finally {
httpClient.getConnectionManager().closeExpiredConnections();
httpClient.getConnectionManager().closeIdleConnections(30,
TimeUnit.SECONDS);
}
return responseJson;
}
/**
*
*
* PUT方式调用
*
*
*
*
*
* @param url
*
*
* @param params
* 参数为NameValuePair键值对对象
*
*
* @return 响应字符串
*
*
* @throws java.io.UnsupportedEncodingException
*/
public String executeByPUT(String url, List<NameValuePair> params) {
HttpPut put = new HttpPut(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseJson = null;
try {
if (params != null) {
put.setEntity(new UrlEncodedFormEntity(params));
}
responseJson = httpClient.execute(put, responseHandler);
log.debug(“HttpClient PUT请求结果:” + responseJson);
}
catch (Exception e) {
log.error(“HttpClient PUT请求异常:”, e);
}
finally {
httpClient.getConnectionManager().closeExpiredConnections();
httpClient.getConnectionManager().closeIdleConnections(30,
TimeUnit.SECONDS);
}
return responseJson;
}
/**
*
*
* DEL方式调用
*
*
*
*
*
* @param url
*
*
* @param params
* 参数为NameValuePair键值对对象
*
*
* @return 响应字符串
*
*
* @throws java.io.UnsupportedEncodingException
*/
public String executeByDEL(String url, List<NameValuePair> params) {
HttpDeleteWithBody del = new HttpDeleteWithBody(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseJson = null;
try {
if (params != null) {
del.setEntity(new UrlEncodedFormEntity(params));
;
}
responseJson = httpClient.execute(del, responseHandler);
log.debug(“HttpClient DEL请求结果:” + responseJson);
}
catch (Exception e) {
log.error(“HttpClient DEL请求异常:”, e);
}
finally {
httpClient.getConnectionManager().closeExpiredConnections();
httpClient.getConnectionManager().closeIdleConnections(30,
TimeUnit.SECONDS);
}
return responseJson;
}
}
class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
@Override
public String getMethod() {
// TODO Auto-generated method stub
return null;
}
public HttpDeleteWithBody(final String uri) {
super();
setURI(URI.create(uri));
}
public HttpDeleteWithBody(final URI uri) {
super();
setURI(uri);
}
public HttpDeleteWithBody() {
super();
}
}