import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HttpClientUtils {
private static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
private static final String ENCODING = "utf-8";
/**
* 发送 get请求
*/
public static String get(String url,String encoding,String headerKeyValue) {
encoding = null == encoding ? ENCODING : encoding;
CloseableHttpClient httpclient = HttpClients.createDefault();
String responseStr = null;
try {
// 创建httpget.
HttpGet httpget = new HttpGet(url);
if(!StringUtils.isEmpty(headerKeyValue)){
String[] headerKeyValueArr = headerKeyValue.split(";");
for (String keyValue : headerKeyValueArr) {
String[] KeyValueArr = keyValue.split("=");
if("Content-Type".equals(KeyValueArr[0]))
httpget.addHeader(KeyValueArr[0], KeyValueArr[1]+";charset="+encoding);
else
httpget.addHeader(KeyValueArr[0], KeyValueArr[1]);
}
}
logger.info("executing request :" + httpget.getURI());
// 执行get请求.
CloseableHttpResponse response = httpclient.execute(httpget);
// 打印响应状态
System.out.println("-------- :"+response.getStatusLine());
int responseCode = response.getStatusLine().getStatusCode();
if(responseCode != 200 ) return responseStr;
// 获取响应实体
HttpEntity entity = response.getEntity();
if (entity == null) return responseStr;
responseStr = EntityUtils.toString(entity,encoding);
// 打印响应内容长度
logger.info("Response content length: " + entity.getContentLength());
// 打印响应内容
logger.info("Response content: " +responseStr );
return responseStr;
} catch (ClientProtocolException e) {
logger.error("........httpget request error : " + e);
e.printStackTrace();
} catch (ParseException ee) {
logger.error("........httpget request error : " + ee);
ee.printStackTrace();
} catch (IOException eee) {
logger.error("........httpget request error : " + eee);
eee.printStackTrace();
} finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return responseStr;
}
/**
* 发送 post请求
*/
public static String post(String url ,List<NameValuePair> params,String encoding,String headerKeyValue) {
encoding = null == encoding ? ENCODING : encoding;
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = HttpClients.createDefault();
String responseStr = null;
try {
// 创建httppost
HttpPost httppost = new HttpPost(url);
if(!StringUtils.isEmpty(headerKeyValue)){
String[] headerKeyValueArr = headerKeyValue.split(";");
for (String keyValue : headerKeyValueArr) {
String[] KeyValueArr = keyValue.split("=");
if("Content-Type".equals(KeyValueArr[0]))
httppost.addHeader(KeyValueArr[0], KeyValueArr[1]+";charset="+encoding);
else
httppost.addHeader(KeyValueArr[0], KeyValueArr[1]);
}
}
UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(params, encoding);
httppost.setEntity(uefEntity);
logger.info("executing request :" + httppost.getURI());
CloseableHttpResponse response = httpclient.execute(httppost);
// 打印响应状态
System.out.println("-------- :"+response.getStatusLine());
int responseCode = response.getStatusLine().getStatusCode();
if(responseCode != 200 ) return responseStr;
HttpEntity entity = response.getEntity();
if (entity == null) return responseStr;
responseStr = EntityUtils.toString(entity, encoding);
logger.info("Response content : " + responseStr);
return responseStr;
} catch (ClientProtocolException e) {
logger.error("........httppost request error : " + e);
e.printStackTrace();
} catch (UnsupportedEncodingException ee) {
logger.error("........httppost request error : " + ee);
ee.printStackTrace();
} catch (IOException eee) {
logger.error("........httppost request error : " + eee);
eee.printStackTrace();
} finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return responseStr;
}
}