package com.yql.sdk.util;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.*;
/**
* HTTP工具类
*
* @author yql
* @date 2018年2月6日
*/
public class HttpUtils {
static Loggerlogger = LoggerFactory.getLogger(HttpUtils.class);
private static PoolingHttpClientConnectionManagerconnMgr;
private static RequestConfigrequestConfig;
private final static int MAX_TIMEOUT =7000;
static {
// 设置连接池
connMgr =new PoolingHttpClientConnectionManager();
// 设置连接池大小
connMgr.setMaxTotal(100);
connMgr.setDefaultMaxPerRoute(connMgr.getMaxTotal());
connMgr.setValidateAfterInactivity(1000);
RequestConfig.Builder configBuilder = RequestConfig.custom();
// 设置连接超时
configBuilder.setConnectTimeout(MAX_TIMEOUT);
// 设置读取超时
configBuilder.setSocketTimeout(MAX_TIMEOUT);
// 设置从连接池获取连接实例的超时
configBuilder.setConnectionRequestTimeout(MAX_TIMEOUT);
requestConfig = configBuilder.build();
}
/**
* 发送 GET 请求(HTTP),不带输入数据
*
* @param url
* @return
*/
public static String doGet(String url) {
return doGet(url,new HashMap());
}
/**
* 发送 GET 请求(HTTP),K-V形式
*
* @param url
* @param params
* @return
*/
public static String doGet(String url, Map params) {
String apiUrl = url;
StringBuffer param =new StringBuffer();
int i =0;
for (String key : params.keySet()) {
if (i ==0)
param.append("?");
else
param.append("&");
param.append(key).append("=").append(params.get(key));
i++;
}
apiUrl += param;
String result =null;
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response =null;
try {
HttpGet httpGet =new HttpGet(apiUrl);
response = httpclient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
return null;
}
HttpEntity entity = response.getEntity();
if (entity !=null) {
result = EntityUtils.toString(entity,"UTF-8");
}
}catch (IOException e) {
e.printStackTrace();
}finally {
if (response !=null) {
try {
EntityUtils.consume(response.getEntity());
}catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
* 发送 POST 请求(HTTP),不带输入数据
*
* @param url
* @return
*/
public static String doPost(String url) {
return doPost(url,new HashMap());
}
/**
* 发送 POST 请求(HTTP),带字符串
*
* @param
* @author yql
* @date 2018年2月6日
*/
public static String doPost(String url, String str) {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response =null;
String result =null;
HttpPost httpPost =new HttpPost(url);
try {
httpPost.setConfig(requestConfig);
httpPost.setEntity(new StringEntity(str, Charset.forName("UTF-8")));
response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
return null;
}
HttpEntity entity = response.getEntity();
if (entity !=null) {
result = EntityUtils.toString(entity,"UTF-8");
}
}catch (IOException e) {
logger.error("发送 POST请求失败,原因:{}", ExceptionUtils.getExceptionMsg(e));
}finally {
if (response !=null) {
try {
EntityUtils.consume(response.getEntity());
}catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
* 发送 POST 请求(HTTP),K-V形式
*
* @param
* @author yql
* @date 2018年2月6日
*/
public static String doPost(String url, Map params) {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response =null;
String result =null;
HttpPost httpPost =new HttpPost(url);
try {
httpPost.setConfig(requestConfig);
List pairList =new ArrayList<>(params.size());
for (Map.Entry entry : params.entrySet()) {
NameValuePair pair =new BasicNameValuePair(entry.getKey(), entry.getValue().toString());
pairList.add(pair);
}
httpPost.setEntity(new UrlEncodedFormEntity(pairList, Charset.forName("UTF-8")));
httpPost.setHeader("Content-Type","application/json");
response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
return null;
}
HttpEntity entity = response.getEntity();
if (entity !=null) {
result = EntityUtils.toString(entity,"UTF-8");
}
}catch (IOException e) {
e.printStackTrace();
}finally {
if (response !=null) {
try {
EntityUtils.consume(response.getEntity());
}catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
* 发送 POST 请求(HTTP),JSON形式
*
* @param
* @author yql
* @date 2018年2月6日
*/
public static String doPost(String url, Object json) {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response =null;
String result =null;
HttpPost httpPost =new HttpPost(url);
try {
httpPost.setConfig(requestConfig);
StringEntity stringEntity =new StringEntity(json.toString(),"UTF-8");// 解决中文乱码问题
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
return null;
}
HttpEntity entity = response.getEntity();
if (entity !=null) {
result = EntityUtils.toString(entity,"UTF-8");
}
}catch (IOException e) {
e.printStackTrace();
}finally {
if (response !=null) {
try {
EntityUtils.consume(response.getEntity());
}catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
* post json 数据
* move from pdsdk ,origin author:gs
*
* @param url
* @param json
* @return
*/
public static String doPostJson(String url, Object json) {
// 创建Httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response =null;
String resultString ="";
try {
// 创建Http Post请求
HttpPost httpPost =new HttpPost(url);
// 创建请求内容
StringEntity stringEntity =new StringEntity(json.toString(),"UTF-8");
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
// 执行http请求
response = httpClient.execute(httpPost);
resultString = EntityUtils.toString(response.getEntity(),"utf-8");
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (response !=null) {
response.close();
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return resultString;
}
/**
* 发送 SSL POST 请求(HTTPS),K-V形式
*
* @param
* @author yql
* @date 2018年2月6日
*/
public static String doPostSSL(String url, Map params) {
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory())
.setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build();
HttpPost httpPost =new HttpPost(url);
CloseableHttpResponse response =null;
String result =null;
try {
httpPost.setConfig(requestConfig);
List pairList =new ArrayList(params.size());
for (Map.Entry entry : params.entrySet()) {
NameValuePair pair =new BasicNameValuePair(entry.getKey(), entry.getValue().toString());
pairList.add(pair);
}
httpPost.setEntity(new UrlEncodedFormEntity(pairList, Charset.forName("UTF-8")));
response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
return null;
}
HttpEntity entity = response.getEntity();
if (entity !=null) {
result = EntityUtils.toString(entity,"UTF-8");
}
}catch (Exception e) {
e.printStackTrace();
}finally {
if (response !=null) {
try {
EntityUtils.consume(response.getEntity());
}catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
* 发送 SSL POST 请求(HTTPS),JSON形式
*
* @param
* @author yql
* @date 2018年2月6日
*/
public static String doPostSSL(String url, Object json) {
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory())
.setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build();
HttpPost httpPost =new HttpPost(url);
CloseableHttpResponse response =null;
String result =null;
try {
httpPost.setConfig(requestConfig);
StringEntity stringEntity =new StringEntity(json.toString(),"UTF-8");// 解决中文乱码问题
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
httpPost.setEntity(stringEntity);
response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
return null;
}
HttpEntity entity = response.getEntity();
if (entity !=null) {
result = EntityUtils.toString(entity,"UTF-8");
}
}catch (Exception e) {
e.printStackTrace();
}finally {
if (response !=null) {
try {
EntityUtils.consume(response.getEntity());
}catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
* 创建SSL安全连接
*
* @return
*/
private static SSLConnectionSocketFactory createSSLConnSocketFactory() {
SSLConnectionSocketFactory sslsf = SSLConnectionSocketFactory.getSocketFactory();
return sslsf;
}
/**
* 模拟表单上传文件
* move from pdsdk ,origin author:gs
*
* @param postFile
* @param postUrl
* @param postParam
* @return
* @throws Exception
*/
public static JSONObject postWithFile(File postFile, String postUrl, Map postParam)throws Exception {
JSONObject res =new JSONObject();
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
//把一个普通参数和文件上传给下面这个地址 是一个servlet
HttpPost httpPost =new HttpPost(postUrl);
//把文件转换成流对象FileBody
FileBody fundFileBin =new FileBody(postFile);
//设置传输参数
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.addPart(postFile.getName(), fundFileBin);//相当于
if (!CollectionUtils.isEmpty(postParam)) {
//设计文件以外的参数
Set keySet = postParam.keySet();
for (String key : keySet) {
//相当于
multipartEntity.addPart(key,new StringBody(postParam.get(key), ContentType.create("text/plain", Consts.UTF_8)));
}
}
HttpEntity reqEntity = multipartEntity.build();
httpPost.setEntity(reqEntity);
logger.info("发起请求的页面地址 " + httpPost.getRequestLine());
//发起请求 并返回请求的响应
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
logger.info("----------------------------------------");
//打印响应状态
logger.info(response.getStatusLine().toString());
res.put("statusCode", response.getStatusLine().getStatusCode());
//获取响应对象
HttpEntity resEntity = response.getEntity();
if (resEntity !=null) {
//打印响应长度
logger.info("Response content length: " + resEntity.getContentLength());
//打印响应内容
res.put("data", EntityUtils.toString(resEntity, Charset.forName("UTF-8")));
}
//销毁
EntityUtils.consume(resEntity);
}catch (Exception e) {
e.printStackTrace();
}finally {
response.close();
}
}catch (ClientProtocolException e1) {
e1.printStackTrace();
}catch (IOException e1) {
e1.printStackTrace();
}finally {
try {
httpClient.close();
}catch (IOException e) {
e.printStackTrace();
}
}
logger.info("uploadFileByHTTP result:" + res);
return res;
}
/**
* 原生字符串发送put请求
*
* @param url
* @param token
* @param jsonStr
* @return
* @author guos
* @date 2019/6/17 17:46
**/
public static String doPut(String url, String token, String jsonStr) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPut httpPut =new HttpPut(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
httpPut.setConfig(requestConfig);
httpPut.setHeader("Content-type","application/json");
httpPut.setHeader("DataEncoding","UTF-8");
httpPut.setHeader("access-token", token);
CloseableHttpResponse httpResponse =null;
try {
httpPut.setEntity(new StringEntity(jsonStr));
httpResponse = httpClient.execute(httpPut);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
return result;
}catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if (httpResponse !=null) {
try {
httpResponse.close();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (null != httpClient) {
try {
httpClient.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
/**
* 发送delete请求
*
* @param url
* @param token
* @param jsonStr
* @return
* @author yql
* @date 2019/6/17 17:47
**/
public static String doDelete(String url, String token, String jsonStr) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpDelete httpDelete =new HttpDelete(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
httpDelete.setConfig(requestConfig);
httpDelete.setHeader("Content-type","application/json");
httpDelete.setHeader("DataEncoding","UTF-8");
httpDelete.setHeader("access-token", token);
CloseableHttpResponse httpResponse =null;
try {
httpResponse = httpClient.execute(httpDelete);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
return result;
}catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if (httpResponse !=null) {
try {
httpResponse.close();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (null != httpClient) {
try {
httpClient.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
/**
* 发送微信postWechatWithFile请求
*
**/
public static JSONObject postWechatWithFile(File postFile, String postUrl, Map postParam)throws Exception {
JSONObject res =new JSONObject();
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
//把一个普通参数和文件上传给下面这个地址 是一个servlet
HttpPost httpPost =new HttpPost(postUrl);
//把文件转换成流对象FileBody
FileBody fundFileBin =new FileBody(postFile);
//设置传输参数
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
multipartEntity.addPart(postFile.getName(), fundFileBin);//相当于
multipartEntity.addBinaryBody("media",postFile, ContentType.MULTIPART_FORM_DATA,postFile.getPath());
if (!CollectionUtils.isEmpty(postParam)) {
//设计文件以外的参数
Set keySet = postParam.keySet();
for (String key : keySet) {
//相当于
multipartEntity.addPart(key,new StringBody(postParam.get(key), ContentType.create("text/plain", Consts.UTF_8)));
}
}
HttpEntity reqEntity = multipartEntity.build();
httpPost.setEntity(reqEntity);
logger.info("发起请求的页面地址 " + httpPost.getRequestLine());
//发起请求 并返回请求的响应
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
logger.info("----------------------------------------");
//打印响应状态
logger.info(response.getStatusLine().toString());
res.put("statusCode", response.getStatusLine().getStatusCode());
//获取响应对象
HttpEntity resEntity = response.getEntity();
if (resEntity !=null) {
//打印响应长度
logger.info("Response content length: " + resEntity.getContentLength());
//打印响应内容
res.put("data", EntityUtils.toString(resEntity, Charset.forName("UTF-8")));
}
//销毁
EntityUtils.consume(resEntity);
}catch (Exception e) {
e.printStackTrace();
}finally {
response.close();
}
}catch (ClientProtocolException e1) {
e1.printStackTrace();
}catch (IOException e1) {
e1.printStackTrace();
}finally {
try {
httpClient.close();
}catch (IOException e) {
e.printStackTrace();
}
}
logger.info("uploadFileByHTTP result:" + res);
return res;
}
}