写道
package com.ycommon.utils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.ConnectionPoolTimeoutException;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
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.protocol.HttpContext;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.SSLInitializationException;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import javax.net.ssl.*;
import java.io.*;
import java.net.SocketTimeoutException;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.*;
/**
* 基于HttpClient实现的Http请求工具
*/
@Slf4j
public class HttpRequestUtils {
//连接池
private static PoolingHttpClientConnectionManager connManager;
//编码
private static final String ENCODING = "UTF-8";
public static final String METHOD_POST = "POST";
public static final String METHOD_GET = "GET";
//出错返回结果
private static final String RESULT = "-1";
//初始化连接池管理器,配置SSL
static {
if (connManager == null) {
try {
/*
* 获取创建ssl上下文对象
* 创建ssl安全访问连接
* 使用带证书的定制SSL访问
*/
File authFile = new File("C:/Users/lynch/Desktop/my.keystore");
SSLContext sslContext = getSSLContext(true, authFile, "mypassword");
// 注册
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", new SSLConnectionSocketFactory(sslContext))
.build();
// ssl注册到连接池
connManager = new PoolingHttpClientConnectionManager(registry);
connManager.setMaxTotal(1000); // 连接池最大连接数
connManager.setDefaultMaxPerRoute(400); // 每个路由最大连接数
} catch (Exception e) {
log.error("HttpRequestUtils e:{}", e);
}
}
}
/**
* 获取客户端连接对象
*
* @param timeOut 超时时间
* @return
*/
private static CloseableHttpClient getHttpClient ( Integer timeOut, HttpHost proxy, boolean isProxy ) {
// 配置请求参数
RequestConfig requestConfig = null;
if (isProxy) {
requestConfig = RequestConfig.custom()
.setProxy(proxy)
.setConnectionRequestTimeout(timeOut)
.setConnectTimeout(timeOut)
.setSocketTimeout(timeOut)
.build();
} else {
requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(timeOut)
.setConnectTimeout(timeOut)
.setSocketTimeout(timeOut)
.build();
}
// 配置超时回调机制
HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() {
public boolean retryRequest ( IOException exception, int executionCount, HttpContext context ) {
if (executionCount >= 3) {// 如果已经重试了3次,就放弃
return false;
}
if (exception instanceof NoHttpResponseException) {// 如果服务器丢掉了连接,那么就重试
return true;
}
if (exception instanceof SSLHandshakeException) {// 不要重试SSL握手异常
return false;
}
if (exception instanceof InterruptedIOException) {// 超时
return true;
}
if (exception instanceof UnknownHostException) {// 目标服务器不可达
return false;
}
if (exception instanceof ConnectTimeoutException) {// 连接被拒绝
return false;
}
if (exception instanceof SSLException) {// ssl握手异常
return false;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
// 如果请求是幂等的,就再次尝试
if (!(request instanceof HttpEntityEnclosingRequest)) {
return true;
}
return false;
}
};
CloseableHttpClient httpClient = HttpClients.custom()
.setDefaultRequestConfig(requestConfig)
.setConnectionManager(connManager)
.setRetryHandler(retryHandler)
.build();
return httpClient;
}
/**
* 获取SSL上下文对象,用来构建SSL Socket连接
*
* @param isDeceive 是否绕过SSL
* @param creFile 整数文件,isDeceive为true 可传null
* @param crePwd 整数密码,isDeceive为true 可传null, 空字符为没有密码
* @return SSL上下文对象
* @throws KeyManagementException
* @throws NoSuchAlgorithmException
* @throws KeyStoreException
* @throws IOException
* @throws FileNotFoundException
* @throws CertificateException
*/
private static SSLContext getSSLContext ( boolean isDeceive, File creFile, String crePwd ) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, CertificateException, FileNotFoundException, IOException {
SSLContext sslContext = null;
if (isDeceive) {
sslContext = SSLContext.getInstance("SSLv3");
// 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
X509TrustManager x509TrustManager = new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers () {
return null;
}
@Override
public void checkServerTrusted ( X509Certificate[] chain, String authType ) throws CertificateException {
}
@Override
public void checkClientTrusted ( X509Certificate[] chain, String authType ) throws CertificateException {
}
};
sslContext.init(null, new TrustManager[]{x509TrustManager}, null);
} else {
if (null != creFile && creFile.length() > 0) {
if (null != crePwd) {
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(new FileInputStream(creFile), crePwd.toCharArray());
sslContext = SSLContexts.custom().loadTrustMaterial(keyStore, new TrustSelfSignedStrategy()).build();
} else {
throw new SSLHandshakeException("整数密码为空");
}
}
}
return sslContext;
}
/**
* post请求,支持SSL
*
* @param url 请求地址
* @param headers 请求头信息
* @param params 请求参数
* @param timeOut 超时时间(毫秒):从连接池获取连接的时间,请求时间,响应时间
* @param isStream 是否以流的方式获取响应信息
* @param clientContext Http请求客户端上下文对象,包含Cookie
* @return 响应信息
* @throws UnsupportedEncodingException
*/
public static String httpPost ( String url, Map<String, Object> headers, Map<String, Object> params, Integer timeOut, boolean isStream, HttpClientContext clientContext, HttpHost proxy, boolean isProxy ) throws Exception {
// 创建post请求
HttpPost httpPost = new HttpPost(url);
// 添加请求头信息
if (null != headers) {
for (Map.Entry<String, Object> entry : headers.entrySet()) {
httpPost.addHeader(entry.getKey(), entry.getValue().toString());
}
}
// 添加请求参数信息
if (null != params) {
httpPost.setEntity(new UrlEncodedFormEntity(covertParams2NVPS(params), ENCODING));
}
return getResult(httpPost, timeOut, isStream, clientContext, proxy, isProxy);
}
/**
* post请求,支持SSL
*
* @param url 请求地址
* @param params 请求参数
* @param timeOut 超时时间(毫秒):从连接池获取连接的时间,请求时间,响应时间
* @param clientContext Http请求客户端上下文对象,包含Cookie
* @return 响应信息
* @throws UnsupportedEncodingException
*/
public static String httpPost ( String url, Map<String, Object> params, Integer timeOut, HttpClientContext clientContext, HttpHost proxy, boolean isProxy ) throws Exception {
// 创建post请求
HttpPost httpPost = new HttpPost(url);
// 添加请求参数信息
if (null != params) {
httpPost.setEntity(new UrlEncodedFormEntity(covertParams2NVPS(params), ENCODING));
}
return getResult(httpPost, timeOut, true, clientContext, proxy, isProxy);
}
/**
* post请求,支持SSL
*
* @param url 请求地址
* @param headers 请求头信息
* @param params 请求参数
* @param timeOut 超时时间(毫秒):从连接池获取连接的时间,请求时间,响应时间
* @param isStream 是否以流的方式获取响应信息
* @param clientContext Http请求客户端上下文对象,包含Cookie
* @return 响应信息
* @throws UnsupportedEncodingException
*/
public static String httpPost ( String url, JSONObject headers, JSONObject params, Integer timeOut, boolean isStream, HttpClientContext clientContext, HttpHost proxy, boolean isProxy ) throws Exception {
// 创建post请求
HttpPost httpPost = new HttpPost(url);
// 添加请求头信息
if (null != headers) {
for (Map.Entry<String, Object> entry : headers.entrySet()) {
httpPost.addHeader(entry.getKey(), entry.getValue().toString());
}
}
// 添加请求参数信息
if (null != params) {
httpPost.setEntity(new UrlEncodedFormEntity(covertParams2NVPS(params), ENCODING));
}
return getResult(httpPost, timeOut, isStream, clientContext, proxy, isProxy);
}
/**
* post请求,支持SSL
*
* @param url 请求地址
* @param params 请求参数
* @param timeOut 超时时间(毫秒):从连接池获取连接的时间,请求时间,响应时间
* @param clientContext Http请求客户端上下文对象,包含Cookie
* @return 响应信息
* @throws UnsupportedEncodingException
*/
public static String httpPost ( String url, JSONObject params, Integer timeOut, HttpClientContext clientContext, HttpHost proxy, boolean isProxy ) throws Exception {
// 创建post请求
HttpPost httpPost = new HttpPost(url);
// 添加请求参数信息
if (null != params) {
httpPost.setEntity(new UrlEncodedFormEntity(covertParams2NVPS(params), ENCODING));
}
return getResult(httpPost, timeOut, true, clientContext, proxy, isProxy);
}
/**
* get请求,支持SSL
*
* @param url 请求地址
* @param headers 请求头信息
* @param params 请求参数
* @param timeOut 超时时间(毫秒):从连接池获取连接的时间,请求时间,响应时间
* @param isStream 是否以流的方式获取响应信息
* @param clientContext Http请求客户端上下文对象,包含Cookie
* @return 响应信息
* @throws URISyntaxException
*/
public static String httpGet ( String url, Map<String, Object> headers, Map<String, Object> params, Integer timeOut, boolean isStream, HttpClientContext clientContext, HttpHost proxy, boolean isProxy ) throws Exception {
// 构建url
URIBuilder uriBuilder = new URIBuilder(url);
// 添加请求参数信息
if (null != params) {
uriBuilder.setParameters(covertParams2NVPS(params));
}
// 创建post请求
HttpGet httpGet = new HttpGet(url);
// 添加请求头信息
if (null != headers) {
for (Map.Entry<String, Object> entry : headers.entrySet()) {
httpGet.addHeader(entry.getKey(), entry.getValue().toString());
}
}
return getResult(httpGet, timeOut, isStream, clientContext, proxy, isProxy);
}
/**
* get请求,支持SSL
*
* @param url 请求地址
* @param params 请求参数
* @param timeOut 超时时间(毫秒):从连接池获取连接的时间,请求时间,响应时间
* @param clientContext Http请求客户端上下文对象,包含Cookie
* @return 响应信息
* @throws URISyntaxException
*/
public static String httpGet ( String url, Map<String, Object> params, Integer timeOut, HttpClientContext clientContext, HttpHost proxy, boolean isProxy ) throws Exception {
// 构建url
URIBuilder uriBuilder = new URIBuilder(url);
// 添加请求参数信息
if (null != params) {
uriBuilder.setParameters(covertParams2NVPS(params));
}
// 创建post请求
HttpGet httpGet = new HttpGet(url);
return getResult(httpGet, timeOut, true, clientContext, proxy, isProxy);
}
/**
* get请求,支持SSL
*
* @param url 请求地址
* @param headers 请求头信息
* @param params 请求参数
* @param timeOut 超时时间(毫秒):从连接池获取连接的时间,请求时间,响应时间
* @param isStream 是否以流的方式获取响应信息
* @param clientContext Http请求客户端上下文对象,包含Cookie
* @return 响应信息
* @throws URISyntaxException
*/
public static String httpGet ( String url, JSONObject headers, JSONObject params, Integer timeOut, boolean isStream, HttpClientContext clientContext, HttpHost proxy, boolean isProxy ) throws Exception {
// 构建url
URIBuilder uriBuilder = new URIBuilder(url);
HttpGet httpGet = new HttpGet(url);
// 添加请求参数信息
if (null != params) {
uriBuilder.setParameters(covertParams2NVPS(params));
}
// 创建get请求
// 添加请求头信息
if (null != headers) {
for (Map.Entry<String, Object> entry : headers.entrySet()) {
httpGet.addHeader(entry.getKey(), entry.getValue().toString());
}
}
return getResult(httpGet, timeOut, isStream, clientContext, proxy, isProxy);
}
/**
* get请求,支持SSL
*
* @param url 请求地址
* @param params 请求参数
* @param timeOut 超时时间(毫秒):从连接池获取连接的时间,请求时间,响应时间
* @param clientContext Http请求客户端上下文对象,包含Cookie
* @return 响应信息
* @throws URISyntaxException
*/
public static String httpGet ( String url, JSONObject params, Integer timeOut, HttpClientContext clientContext, HttpHost proxy, boolean isProxy ) throws Exception {
// 构建url
URIBuilder uriBuilder = new URIBuilder(url);
// 添加请求参数信息
if (null != params) {
uriBuilder.setParameters(covertParams2NVPS(params));
}
// 创建get请求
HttpGet httpGet = new HttpGet(url);
return getResult(httpGet, timeOut, true, clientContext, proxy, isProxy);
}
private static String getResult ( HttpRequestBase httpRequest, Integer timeOut, boolean isStream, HttpClientContext clientContext, HttpHost proxy, boolean isProxy ) throws Exception {
// 响应结果
StringBuilder sb = null;
CloseableHttpResponse response = null;
CloseableHttpClient httpClient = null;
try {
// 获取连接客户端
httpClient = getHttpClient(timeOut, proxy, isProxy);
// 发起请求
if (null != clientContext) {
response = httpClient.execute(httpRequest, clientContext);
} else {
response = httpClient.execute(httpRequest);
}
int respCode = response.getStatusLine().getStatusCode();
// 如果是重定向
if (302 == respCode || 301 == respCode) {
String locationUrl = response.getLastHeader("Location").getValue();
return getResult(new HttpPost(locationUrl), timeOut, isStream, clientContext, proxy, isProxy);
}
// 正确响应
//if (200 == respCode) {
// 获得响应实体
HttpEntity entity = response.getEntity();
sb = new StringBuilder();
// 如果是以流的形式获取
if (isStream) {
// 包装成高效流
BufferedInputStream bis = new BufferedInputStream(entity.getContent());
BufferedReader br = new BufferedReader(new InputStreamReader(bis, ENCODING));
String len = "";
while ((len = br.readLine()) != null) {
sb.append(len);
}
} else {
sb.append(EntityUtils.toString(entity, ENCODING));
if (sb.length() < 1) {
sb.append("-1");
}
}
//}
} catch (ConnectionPoolTimeoutException e) {
log.error("从连接池获取连接超时!!!");
e.printStackTrace();
} catch (SocketTimeoutException e) {
log.error("响应超时");
e.printStackTrace();
} catch (ConnectTimeoutException e) {
log.error("请求超时");
e.printStackTrace();
} catch (ClientProtocolException e) {
log.error("http协议错误");
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
log.error("不支持的字符编码");
e.printStackTrace();
} catch (UnsupportedOperationException e) {
log.error("不支持的请求操作");
e.printStackTrace();
} catch (ParseException e) {
log.error("解析错误");
e.printStackTrace();
} catch (IOException e) {
log.error("IO错误");
e.printStackTrace();
} finally {
if (null != response) {
try {
response.close();
} catch (IOException e) {
log.error("关闭响应连接出错");
e.printStackTrace();
}
}
if (httpClient != null)
httpClient.close();
}
return sb == null ? RESULT : ("".equals(sb.toString().trim()) ? "-1" : sb.toString());
}
/**
* Map转换成NameValuePair List集合
*
* @param params map
* @return NameValuePair List集合
*/
public static List<NameValuePair> covertParams2NVPS ( Map<String, Object> params ) {
List<NameValuePair> paramList = new LinkedList<>();
for (Map.Entry<String, Object> entry : params.entrySet()) {
paramList.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
return paramList;
}
/**
* @param map 数据为map 范型 为Map<String, String>
* @param url
* @param method
* @param timeout
* @return
* @throws Exception
*/
public static String sendRequestMethod ( Map<String, String> map, String url, String method, int timeout, HttpHost proxy, boolean isProxy )
throws Exception {
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = getHttpClient(timeout, proxy, isProxy);
CloseableHttpResponse response = null;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
if (map != null) {
Set<Map.Entry<String, String>> entrySet = map.entrySet();
for (Map.Entry<String, String> e : entrySet) {
String name = e.getKey();
String value = e.getValue();
NameValuePair pair = new BasicNameValuePair(name, value);
params.add(pair);
}
}
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(params, "UTF-8");
if (log.isDebugEnabled()) {
log.debug("http client url:" + url);
log.debug("http client params:" + params.toString());
}
log.info("http client url:" + url);
log.info("http client params:" + params.toString());
HttpUriRequest reqMethod = null;
if (StringUtils.equalsIgnoreCase(METHOD_POST, method)) {
reqMethod = RequestBuilder.post().setUri(url).setEntity(urlEncodedFormEntity).build();
} else if (METHOD_GET.equalsIgnoreCase(method)) {
reqMethod = RequestBuilder.get().setUri(url).setEntity(urlEncodedFormEntity)
.build();
} else {
log.warn("method unknow, return null.");
return null;
}
if (httpclient != null)
response = httpclient.execute(reqMethod);
if (response != null && response.getStatusLine().getStatusCode() == 200)
return EntityUtils.toString(response.getEntity(), "UTF-8");
else {
if (response != null)
log.warn("http response status error, status{}, return null"
+ response.getStatusLine().getStatusCode());
return null;
}
} catch (Exception e) {
log.error(e.getMessage());
throw e;
} finally {
if (response != null)
response.close();
if (httpclient != null)
httpclient.close();
}
}
/**
* @param map 数据为map 范型 为Map<String, Object> map
* @param url
* @param method 大写 POST 或者 GET
* @return
* @throws Exception
*/
public static String sendRequestMethod ( Map<String, Object> map, String url, String method, HttpHost proxy, boolean isProxy ) throws Exception {
int timeout = 60;
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = getHttpClient(timeout, proxy, isProxy);
CloseableHttpResponse response = null;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
if (map != null) {
Set<Map.Entry<String, Object>> entrySet = map.entrySet();
for (Map.Entry<String, Object> e : entrySet) {
String name = e.getKey();
String value = String.valueOf(e.getValue());
NameValuePair pair = new BasicNameValuePair(name, value);
params.add(pair);
}
}
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(params, "UTF-8");
if (log.isDebugEnabled()) {
log.debug("http client url:" + url);
log.debug("http client params:" + params.toString());
}
log.info("http client params:" + params.toString());
log.info("http client url:" + url);
HttpUriRequest reqMethod = null;
if (METHOD_POST.equalsIgnoreCase(method)) {
reqMethod = RequestBuilder.post().setUri(url).setEntity(urlEncodedFormEntity).build();
} else if (METHOD_GET.equalsIgnoreCase(method)) {
reqMethod = RequestBuilder.get().setUri(url).setEntity(urlEncodedFormEntity).build();
} else {
log.warn("method unknow, return null.");
return null;
}
if (httpclient != null)
response = httpclient.execute(reqMethod);
if (response != null && response.getStatusLine().getStatusCode() == 200)
return EntityUtils.toString(response.getEntity(), "UTF-8");
else {
if (response != null)
log.warn("http response status error, status{}, return null", response.getStatusLine().getStatusCode());
return null;
}
} catch (Exception e) {
log.error(e.getMessage());
throw e;
} finally {
if (response != null)
response.close();
if (httpclient != null)
httpclient.close();
}
}
/**
* 发送Https 并不校验证书
*
* @param url 地址
* @param map 数据 Map<String, Object>
* @return
* @throws Exception
*/
public static String sendRequestNoCheckCerPostMap ( String url, Map<String, Object> map, HttpHost proxy, boolean isProxy ) throws Exception {
int timeout = 60;
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = getHttpClient(timeout, proxy, isProxy);
CloseableHttpResponse response = null;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
if (map != null) {
Set<Map.Entry<String, Object>> entrySet = map.entrySet();
for (Map.Entry<String, Object> e : entrySet) {
String value = String.valueOf(e.getValue());
String name = e.getKey();
NameValuePair pair = new BasicNameValuePair(name, value);
params.add(pair);
}
}
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(params, "UTF-8");
if (log.isDebugEnabled()) {
log.debug("http client params:" + params.toString());
log.debug("http client url:" + url);
}
log.info("http client url:" + url);
log.info("http client params:" + params.toString());
HttpUriRequest reqMethod = null;
if (METHOD_POST.equalsIgnoreCase("POST")) {
reqMethod = RequestBuilder.post().setUri(url)
// .setCharset(java.nio.charset.Charset.forName("UTF-8"))
// .addParameters(params.toArray(new
// BasicNameValuePair[params.size()]))
.setEntity(urlEncodedFormEntity).build();
} /*
* else if(METHOD_GET.equalsIgnoreCase(method)) { reqMethod =
* RequestBuilder.get().setUri(url)
* .setEntity(urlEncodedFormEntity)
* //.addParameters(params.toArray(new
* BasicNameValuePair[params.size()]))
* .setConfig(requestConfig).build(); }
*/ else {
log.warn("method unknow, return null.");
return null;
}
if (httpclient != null)
response = httpclient.execute(reqMethod);
String string = EntityUtils.toString(response.getEntity(), "UTF-8");
log.info("statusCode: " + response.getStatusLine().getStatusCode());
log.info("resp: " + string);
if (response != null && response.getStatusLine().getStatusCode() == 200)
return string;
else {
if (response != null)
log.warn("http response status error, status{}, return null"
+ response.getStatusLine().getStatusCode());
return null;
}
} catch (Exception e) {
log.error(e.getMessage());
throw e;
} finally {
if (response != null)
response.close();
if (httpclient != null)
httpclient.close();
}
}
/**
* 发送Https 并不校验证书
*
* @param url 地址
* @param json 数据
* @return
* @throws Exception
*/
public static String sendRequestNoCheckCerPostJOSNString ( String url, String json, HttpHost proxy, boolean isProxy ) throws Exception {
int timeout = 60;
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = getHttpClient(timeout, proxy, isProxy);
CloseableHttpResponse response = null;
HttpPost httpPost = new HttpPost(url);
try {
StringEntity entity = new StringEntity(json, "utf-8");// 解决中文乱码问题
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
if (log.isDebugEnabled())
log.debug("executing request :{}" + httpPost.getRequestLine());
HttpUriRequest reqMethod = RequestBuilder.post().setUri(url).setEntity(entity).build();
if (httpclient != null)
response = httpclient.execute(reqMethod);
if (response != null && response.getStatusLine().getStatusCode() == 200)
return EntityUtils.toString(response.getEntity(), "UTF-8");
else {
if (response != null)
log.warn(" status code {} " + response.getStatusLine().getStatusCode());
log.warn(" server error, return null");
return null;
}
} catch (Exception e) {
log.error(e.getMessage());
throw e;
} finally {
if (response != null)
response.close();
if (httpclient != null)
httpclient.close();
}
}
/**
* post 请求 json 数据
*
* @param json JSON
* @param url 地址
* @param timeout 设置超时时间
* @return
* @throws Exception
*/
public static String sendJsonRequestMethod ( String json, String url, int timeout, HttpHost proxy, boolean isProxy ) throws Exception {
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = getHttpClient(timeout, proxy, isProxy);
HttpPost httpPost = new HttpPost(url);
CloseableHttpResponse response = null;
try {
StringEntity entity = new StringEntity(json, "utf-8");// 解决中文乱码问题
entity.setContentType("application/json");
entity.setContentEncoding("UTF-8");
httpPost.setEntity(entity);
if (log.isDebugEnabled())
log.debug("executing request :{}" + httpPost.getRequestLine());
HttpUriRequest reqMethod = RequestBuilder.post().setUri(url).setEntity(entity).build();
if (httpclient != null) {
response = httpclient.execute(reqMethod);
}
if (response != null && response.getStatusLine().getStatusCode() == 200) {
return EntityUtils.toString(response.getEntity(), "UTF-8");
} else {
if (response != null)
log.warn(" server error, return null");
log.warn(" status code {} " + response.getStatusLine().getStatusCode());
return null;
}
} catch (Exception e) {
log.error(e.getMessage());
throw e;
} finally {
if (response != null)
response.close();
if (httpclient != null)
httpclient.close();
}
}
/**
* get请求,支持SSL
*
* @param url 请求地址
* @param headers 请求头信息
* @param params 请求参数
* @param timeOut 超时时间(毫秒):从连接池获取连接的时间,请求时间,响应时间
* @param isStream 是否以流的方式获取响应信息
* @param clientContext Http请求客户端上下文对象,包含Cookie
* @return 响应信息
* @throws Exception
*/
public static String httpGetImage ( String url, JSONObject headers, JSONObject params, Integer timeOut, boolean isStream, HttpClientContext clientContext, HttpHost proxy, boolean isProxy, String imagePath ) throws Exception {
// 构建url
URIBuilder uriBuilder = new URIBuilder(url);
HttpGet httpGet = new HttpGet(url);
// 添加请求参数信息
if (null != params) {
uriBuilder.setParameters(covertParams2NVPS(params));
}
// 创建get请求
// 添加请求头信息
if (null != headers) {
for (Map.Entry<String, Object> entry : headers.entrySet()) {
httpGet.addHeader(entry.getKey(), entry.getValue().toString());
}
}
return getResultImage(httpGet, timeOut, isStream, clientContext, proxy, isProxy, imagePath);
}
private static String getResultImage ( HttpRequestBase httpRequest, Integer timeOut, boolean isStream, HttpClientContext clientContext, HttpHost proxy, boolean isProxy, String imagePath ) throws Exception {
// 响应结果
StringBuilder sb = null;
CloseableHttpResponse response = null;
CloseableHttpClient httpClient = null;
try {
// 获取连接客户端
httpClient = getHttpClient(timeOut, proxy, isProxy);
// 发起请求
if (null != clientContext) {
response = httpClient.execute(httpRequest, clientContext);
} else {
response = httpClient.execute(httpRequest);
}
int respCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
sb = new StringBuilder();
// 如果是以流的形式获取
if (isStream) {
String image_path = imagePath + System.currentTimeMillis() + ".png";
FileUtils.copyToFile(entity.getContent(), new File(image_path));
sb.append(image_path);
} else {
sb.append(EntityUtils.toString(entity, ENCODING));
if (sb.length() < 1) {
sb.append("-1");
}
}
//}
} catch (ConnectionPoolTimeoutException e) {
log.error("从连接池获取连接超时!!!");
e.printStackTrace();
} catch (SocketTimeoutException e) {
log.error("响应超时");
e.printStackTrace();
} catch (ConnectTimeoutException e) {
log.error("请求超时");
e.printStackTrace();
} catch (ClientProtocolException e) {
log.error("http协议错误");
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
log.error("不支持的字符编码");
e.printStackTrace();
} catch (UnsupportedOperationException e) {
log.error("不支持的请求操作");
e.printStackTrace();
} catch (ParseException e) {
log.error("解析错误");
e.printStackTrace();
} catch (IOException e) {
log.error("IO错误");
e.printStackTrace();
} finally {
if (null != response) {
try {
response.close();
} catch (IOException e) {
log.error("关闭响应连接出错");
e.printStackTrace();
}
}
if (httpClient != null) {
httpClient.close();
}
}
return sb == null ? RESULT : ("".equals(sb.toString().trim()) ? "-1" : sb.toString());
}
public static void main ( String[] args ) throws Exception {
HttpClientContext clientContext = HttpClientContext.create();
CookieStore cookieStore = new BasicCookieStore();
/* BasicClientCookie cookie = new BasicClientCookie("Hm_lpvt_de769ee64d6270439549be36ba257ff6", "1553933779");
cookie.setVersion(0);
cookie.setDomain(".12123.com");
cookie.setPath("/");
cookieStore.addCookie(cookie);
*/
clientContext.setCookieStore(cookieStore);
/** 登录 */
JSONObject head = JSONObject.parseObject("{\"User-Agent\":\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36\"}");
/* *//** 验证是否登录 *//*
log.info(httpPost("http://localhost/auth/isLogin", null, null, 6000, false, clientContext));
*//** 退出登录 *//*
log.info(httpPost("http://localhost/auth/logout", null, null, 6000, false, clientContext));*/
}
}
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.ConnectionPoolTimeoutException;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
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.protocol.HttpContext;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.SSLInitializationException;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import javax.net.ssl.*;
import java.io.*;
import java.net.SocketTimeoutException;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.*;
/**
* 基于HttpClient实现的Http请求工具
*/
@Slf4j
public class HttpRequestUtils {
//连接池
private static PoolingHttpClientConnectionManager connManager;
//编码
private static final String ENCODING = "UTF-8";
public static final String METHOD_POST = "POST";
public static final String METHOD_GET = "GET";
//出错返回结果
private static final String RESULT = "-1";
//初始化连接池管理器,配置SSL
static {
if (connManager == null) {
try {
/*
* 获取创建ssl上下文对象
* 创建ssl安全访问连接
* 使用带证书的定制SSL访问
*/
File authFile = new File("C:/Users/lynch/Desktop/my.keystore");
SSLContext sslContext = getSSLContext(true, authFile, "mypassword");
// 注册
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", new SSLConnectionSocketFactory(sslContext))
.build();
// ssl注册到连接池
connManager = new PoolingHttpClientConnectionManager(registry);
connManager.setMaxTotal(1000); // 连接池最大连接数
connManager.setDefaultMaxPerRoute(400); // 每个路由最大连接数
} catch (Exception e) {
log.error("HttpRequestUtils e:{}", e);
}
}
}
/**
* 获取客户端连接对象
*
* @param timeOut 超时时间
* @return
*/
private static CloseableHttpClient getHttpClient ( Integer timeOut, HttpHost proxy, boolean isProxy ) {
// 配置请求参数
RequestConfig requestConfig = null;
if (isProxy) {
requestConfig = RequestConfig.custom()
.setProxy(proxy)
.setConnectionRequestTimeout(timeOut)
.setConnectTimeout(timeOut)
.setSocketTimeout(timeOut)
.build();
} else {
requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(timeOut)
.setConnectTimeout(timeOut)
.setSocketTimeout(timeOut)
.build();
}
// 配置超时回调机制
HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() {
public boolean retryRequest ( IOException exception, int executionCount, HttpContext context ) {
if (executionCount >= 3) {// 如果已经重试了3次,就放弃
return false;
}
if (exception instanceof NoHttpResponseException) {// 如果服务器丢掉了连接,那么就重试
return true;
}
if (exception instanceof SSLHandshakeException) {// 不要重试SSL握手异常
return false;
}
if (exception instanceof InterruptedIOException) {// 超时
return true;
}
if (exception instanceof UnknownHostException) {// 目标服务器不可达
return false;
}
if (exception instanceof ConnectTimeoutException) {// 连接被拒绝
return false;
}
if (exception instanceof SSLException) {// ssl握手异常
return false;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
// 如果请求是幂等的,就再次尝试
if (!(request instanceof HttpEntityEnclosingRequest)) {
return true;
}
return false;
}
};
CloseableHttpClient httpClient = HttpClients.custom()
.setDefaultRequestConfig(requestConfig)
.setConnectionManager(connManager)
.setRetryHandler(retryHandler)
.build();
return httpClient;
}
/**
* 获取SSL上下文对象,用来构建SSL Socket连接
*
* @param isDeceive 是否绕过SSL
* @param creFile 整数文件,isDeceive为true 可传null
* @param crePwd 整数密码,isDeceive为true 可传null, 空字符为没有密码
* @return SSL上下文对象
* @throws KeyManagementException
* @throws NoSuchAlgorithmException
* @throws KeyStoreException
* @throws IOException
* @throws FileNotFoundException
* @throws CertificateException
*/
private static SSLContext getSSLContext ( boolean isDeceive, File creFile, String crePwd ) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, CertificateException, FileNotFoundException, IOException {
SSLContext sslContext = null;
if (isDeceive) {
sslContext = SSLContext.getInstance("SSLv3");
// 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
X509TrustManager x509TrustManager = new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers () {
return null;
}
@Override
public void checkServerTrusted ( X509Certificate[] chain, String authType ) throws CertificateException {
}
@Override
public void checkClientTrusted ( X509Certificate[] chain, String authType ) throws CertificateException {
}
};
sslContext.init(null, new TrustManager[]{x509TrustManager}, null);
} else {
if (null != creFile && creFile.length() > 0) {
if (null != crePwd) {
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(new FileInputStream(creFile), crePwd.toCharArray());
sslContext = SSLContexts.custom().loadTrustMaterial(keyStore, new TrustSelfSignedStrategy()).build();
} else {
throw new SSLHandshakeException("整数密码为空");
}
}
}
return sslContext;
}
/**
* post请求,支持SSL
*
* @param url 请求地址
* @param headers 请求头信息
* @param params 请求参数
* @param timeOut 超时时间(毫秒):从连接池获取连接的时间,请求时间,响应时间
* @param isStream 是否以流的方式获取响应信息
* @param clientContext Http请求客户端上下文对象,包含Cookie
* @return 响应信息
* @throws UnsupportedEncodingException
*/
public static String httpPost ( String url, Map<String, Object> headers, Map<String, Object> params, Integer timeOut, boolean isStream, HttpClientContext clientContext, HttpHost proxy, boolean isProxy ) throws Exception {
// 创建post请求
HttpPost httpPost = new HttpPost(url);
// 添加请求头信息
if (null != headers) {
for (Map.Entry<String, Object> entry : headers.entrySet()) {
httpPost.addHeader(entry.getKey(), entry.getValue().toString());
}
}
// 添加请求参数信息
if (null != params) {
httpPost.setEntity(new UrlEncodedFormEntity(covertParams2NVPS(params), ENCODING));
}
return getResult(httpPost, timeOut, isStream, clientContext, proxy, isProxy);
}
/**
* post请求,支持SSL
*
* @param url 请求地址
* @param params 请求参数
* @param timeOut 超时时间(毫秒):从连接池获取连接的时间,请求时间,响应时间
* @param clientContext Http请求客户端上下文对象,包含Cookie
* @return 响应信息
* @throws UnsupportedEncodingException
*/
public static String httpPost ( String url, Map<String, Object> params, Integer timeOut, HttpClientContext clientContext, HttpHost proxy, boolean isProxy ) throws Exception {
// 创建post请求
HttpPost httpPost = new HttpPost(url);
// 添加请求参数信息
if (null != params) {
httpPost.setEntity(new UrlEncodedFormEntity(covertParams2NVPS(params), ENCODING));
}
return getResult(httpPost, timeOut, true, clientContext, proxy, isProxy);
}
/**
* post请求,支持SSL
*
* @param url 请求地址
* @param headers 请求头信息
* @param params 请求参数
* @param timeOut 超时时间(毫秒):从连接池获取连接的时间,请求时间,响应时间
* @param isStream 是否以流的方式获取响应信息
* @param clientContext Http请求客户端上下文对象,包含Cookie
* @return 响应信息
* @throws UnsupportedEncodingException
*/
public static String httpPost ( String url, JSONObject headers, JSONObject params, Integer timeOut, boolean isStream, HttpClientContext clientContext, HttpHost proxy, boolean isProxy ) throws Exception {
// 创建post请求
HttpPost httpPost = new HttpPost(url);
// 添加请求头信息
if (null != headers) {
for (Map.Entry<String, Object> entry : headers.entrySet()) {
httpPost.addHeader(entry.getKey(), entry.getValue().toString());
}
}
// 添加请求参数信息
if (null != params) {
httpPost.setEntity(new UrlEncodedFormEntity(covertParams2NVPS(params), ENCODING));
}
return getResult(httpPost, timeOut, isStream, clientContext, proxy, isProxy);
}
/**
* post请求,支持SSL
*
* @param url 请求地址
* @param params 请求参数
* @param timeOut 超时时间(毫秒):从连接池获取连接的时间,请求时间,响应时间
* @param clientContext Http请求客户端上下文对象,包含Cookie
* @return 响应信息
* @throws UnsupportedEncodingException
*/
public static String httpPost ( String url, JSONObject params, Integer timeOut, HttpClientContext clientContext, HttpHost proxy, boolean isProxy ) throws Exception {
// 创建post请求
HttpPost httpPost = new HttpPost(url);
// 添加请求参数信息
if (null != params) {
httpPost.setEntity(new UrlEncodedFormEntity(covertParams2NVPS(params), ENCODING));
}
return getResult(httpPost, timeOut, true, clientContext, proxy, isProxy);
}
/**
* get请求,支持SSL
*
* @param url 请求地址
* @param headers 请求头信息
* @param params 请求参数
* @param timeOut 超时时间(毫秒):从连接池获取连接的时间,请求时间,响应时间
* @param isStream 是否以流的方式获取响应信息
* @param clientContext Http请求客户端上下文对象,包含Cookie
* @return 响应信息
* @throws URISyntaxException
*/
public static String httpGet ( String url, Map<String, Object> headers, Map<String, Object> params, Integer timeOut, boolean isStream, HttpClientContext clientContext, HttpHost proxy, boolean isProxy ) throws Exception {
// 构建url
URIBuilder uriBuilder = new URIBuilder(url);
// 添加请求参数信息
if (null != params) {
uriBuilder.setParameters(covertParams2NVPS(params));
}
// 创建post请求
HttpGet httpGet = new HttpGet(url);
// 添加请求头信息
if (null != headers) {
for (Map.Entry<String, Object> entry : headers.entrySet()) {
httpGet.addHeader(entry.getKey(), entry.getValue().toString());
}
}
return getResult(httpGet, timeOut, isStream, clientContext, proxy, isProxy);
}
/**
* get请求,支持SSL
*
* @param url 请求地址
* @param params 请求参数
* @param timeOut 超时时间(毫秒):从连接池获取连接的时间,请求时间,响应时间
* @param clientContext Http请求客户端上下文对象,包含Cookie
* @return 响应信息
* @throws URISyntaxException
*/
public static String httpGet ( String url, Map<String, Object> params, Integer timeOut, HttpClientContext clientContext, HttpHost proxy, boolean isProxy ) throws Exception {
// 构建url
URIBuilder uriBuilder = new URIBuilder(url);
// 添加请求参数信息
if (null != params) {
uriBuilder.setParameters(covertParams2NVPS(params));
}
// 创建post请求
HttpGet httpGet = new HttpGet(url);
return getResult(httpGet, timeOut, true, clientContext, proxy, isProxy);
}
/**
* get请求,支持SSL
*
* @param url 请求地址
* @param headers 请求头信息
* @param params 请求参数
* @param timeOut 超时时间(毫秒):从连接池获取连接的时间,请求时间,响应时间
* @param isStream 是否以流的方式获取响应信息
* @param clientContext Http请求客户端上下文对象,包含Cookie
* @return 响应信息
* @throws URISyntaxException
*/
public static String httpGet ( String url, JSONObject headers, JSONObject params, Integer timeOut, boolean isStream, HttpClientContext clientContext, HttpHost proxy, boolean isProxy ) throws Exception {
// 构建url
URIBuilder uriBuilder = new URIBuilder(url);
HttpGet httpGet = new HttpGet(url);
// 添加请求参数信息
if (null != params) {
uriBuilder.setParameters(covertParams2NVPS(params));
}
// 创建get请求
// 添加请求头信息
if (null != headers) {
for (Map.Entry<String, Object> entry : headers.entrySet()) {
httpGet.addHeader(entry.getKey(), entry.getValue().toString());
}
}
return getResult(httpGet, timeOut, isStream, clientContext, proxy, isProxy);
}
/**
* get请求,支持SSL
*
* @param url 请求地址
* @param params 请求参数
* @param timeOut 超时时间(毫秒):从连接池获取连接的时间,请求时间,响应时间
* @param clientContext Http请求客户端上下文对象,包含Cookie
* @return 响应信息
* @throws URISyntaxException
*/
public static String httpGet ( String url, JSONObject params, Integer timeOut, HttpClientContext clientContext, HttpHost proxy, boolean isProxy ) throws Exception {
// 构建url
URIBuilder uriBuilder = new URIBuilder(url);
// 添加请求参数信息
if (null != params) {
uriBuilder.setParameters(covertParams2NVPS(params));
}
// 创建get请求
HttpGet httpGet = new HttpGet(url);
return getResult(httpGet, timeOut, true, clientContext, proxy, isProxy);
}
private static String getResult ( HttpRequestBase httpRequest, Integer timeOut, boolean isStream, HttpClientContext clientContext, HttpHost proxy, boolean isProxy ) throws Exception {
// 响应结果
StringBuilder sb = null;
CloseableHttpResponse response = null;
CloseableHttpClient httpClient = null;
try {
// 获取连接客户端
httpClient = getHttpClient(timeOut, proxy, isProxy);
// 发起请求
if (null != clientContext) {
response = httpClient.execute(httpRequest, clientContext);
} else {
response = httpClient.execute(httpRequest);
}
int respCode = response.getStatusLine().getStatusCode();
// 如果是重定向
if (302 == respCode || 301 == respCode) {
String locationUrl = response.getLastHeader("Location").getValue();
return getResult(new HttpPost(locationUrl), timeOut, isStream, clientContext, proxy, isProxy);
}
// 正确响应
//if (200 == respCode) {
// 获得响应实体
HttpEntity entity = response.getEntity();
sb = new StringBuilder();
// 如果是以流的形式获取
if (isStream) {
// 包装成高效流
BufferedInputStream bis = new BufferedInputStream(entity.getContent());
BufferedReader br = new BufferedReader(new InputStreamReader(bis, ENCODING));
String len = "";
while ((len = br.readLine()) != null) {
sb.append(len);
}
} else {
sb.append(EntityUtils.toString(entity, ENCODING));
if (sb.length() < 1) {
sb.append("-1");
}
}
//}
} catch (ConnectionPoolTimeoutException e) {
log.error("从连接池获取连接超时!!!");
e.printStackTrace();
} catch (SocketTimeoutException e) {
log.error("响应超时");
e.printStackTrace();
} catch (ConnectTimeoutException e) {
log.error("请求超时");
e.printStackTrace();
} catch (ClientProtocolException e) {
log.error("http协议错误");
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
log.error("不支持的字符编码");
e.printStackTrace();
} catch (UnsupportedOperationException e) {
log.error("不支持的请求操作");
e.printStackTrace();
} catch (ParseException e) {
log.error("解析错误");
e.printStackTrace();
} catch (IOException e) {
log.error("IO错误");
e.printStackTrace();
} finally {
if (null != response) {
try {
response.close();
} catch (IOException e) {
log.error("关闭响应连接出错");
e.printStackTrace();
}
}
if (httpClient != null)
httpClient.close();
}
return sb == null ? RESULT : ("".equals(sb.toString().trim()) ? "-1" : sb.toString());
}
/**
* Map转换成NameValuePair List集合
*
* @param params map
* @return NameValuePair List集合
*/
public static List<NameValuePair> covertParams2NVPS ( Map<String, Object> params ) {
List<NameValuePair> paramList = new LinkedList<>();
for (Map.Entry<String, Object> entry : params.entrySet()) {
paramList.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
}
return paramList;
}
/**
* @param map 数据为map 范型 为Map<String, String>
* @param url
* @param method
* @param timeout
* @return
* @throws Exception
*/
public static String sendRequestMethod ( Map<String, String> map, String url, String method, int timeout, HttpHost proxy, boolean isProxy )
throws Exception {
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = getHttpClient(timeout, proxy, isProxy);
CloseableHttpResponse response = null;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
if (map != null) {
Set<Map.Entry<String, String>> entrySet = map.entrySet();
for (Map.Entry<String, String> e : entrySet) {
String name = e.getKey();
String value = e.getValue();
NameValuePair pair = new BasicNameValuePair(name, value);
params.add(pair);
}
}
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(params, "UTF-8");
if (log.isDebugEnabled()) {
log.debug("http client url:" + url);
log.debug("http client params:" + params.toString());
}
log.info("http client url:" + url);
log.info("http client params:" + params.toString());
HttpUriRequest reqMethod = null;
if (StringUtils.equalsIgnoreCase(METHOD_POST, method)) {
reqMethod = RequestBuilder.post().setUri(url).setEntity(urlEncodedFormEntity).build();
} else if (METHOD_GET.equalsIgnoreCase(method)) {
reqMethod = RequestBuilder.get().setUri(url).setEntity(urlEncodedFormEntity)
.build();
} else {
log.warn("method unknow, return null.");
return null;
}
if (httpclient != null)
response = httpclient.execute(reqMethod);
if (response != null && response.getStatusLine().getStatusCode() == 200)
return EntityUtils.toString(response.getEntity(), "UTF-8");
else {
if (response != null)
log.warn("http response status error, status{}, return null"
+ response.getStatusLine().getStatusCode());
return null;
}
} catch (Exception e) {
log.error(e.getMessage());
throw e;
} finally {
if (response != null)
response.close();
if (httpclient != null)
httpclient.close();
}
}
/**
* @param map 数据为map 范型 为Map<String, Object> map
* @param url
* @param method 大写 POST 或者 GET
* @return
* @throws Exception
*/
public static String sendRequestMethod ( Map<String, Object> map, String url, String method, HttpHost proxy, boolean isProxy ) throws Exception {
int timeout = 60;
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = getHttpClient(timeout, proxy, isProxy);
CloseableHttpResponse response = null;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
if (map != null) {
Set<Map.Entry<String, Object>> entrySet = map.entrySet();
for (Map.Entry<String, Object> e : entrySet) {
String name = e.getKey();
String value = String.valueOf(e.getValue());
NameValuePair pair = new BasicNameValuePair(name, value);
params.add(pair);
}
}
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(params, "UTF-8");
if (log.isDebugEnabled()) {
log.debug("http client url:" + url);
log.debug("http client params:" + params.toString());
}
log.info("http client params:" + params.toString());
log.info("http client url:" + url);
HttpUriRequest reqMethod = null;
if (METHOD_POST.equalsIgnoreCase(method)) {
reqMethod = RequestBuilder.post().setUri(url).setEntity(urlEncodedFormEntity).build();
} else if (METHOD_GET.equalsIgnoreCase(method)) {
reqMethod = RequestBuilder.get().setUri(url).setEntity(urlEncodedFormEntity).build();
} else {
log.warn("method unknow, return null.");
return null;
}
if (httpclient != null)
response = httpclient.execute(reqMethod);
if (response != null && response.getStatusLine().getStatusCode() == 200)
return EntityUtils.toString(response.getEntity(), "UTF-8");
else {
if (response != null)
log.warn("http response status error, status{}, return null", response.getStatusLine().getStatusCode());
return null;
}
} catch (Exception e) {
log.error(e.getMessage());
throw e;
} finally {
if (response != null)
response.close();
if (httpclient != null)
httpclient.close();
}
}
/**
* 发送Https 并不校验证书
*
* @param url 地址
* @param map 数据 Map<String, Object>
* @return
* @throws Exception
*/
public static String sendRequestNoCheckCerPostMap ( String url, Map<String, Object> map, HttpHost proxy, boolean isProxy ) throws Exception {
int timeout = 60;
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = getHttpClient(timeout, proxy, isProxy);
CloseableHttpResponse response = null;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
if (map != null) {
Set<Map.Entry<String, Object>> entrySet = map.entrySet();
for (Map.Entry<String, Object> e : entrySet) {
String value = String.valueOf(e.getValue());
String name = e.getKey();
NameValuePair pair = new BasicNameValuePair(name, value);
params.add(pair);
}
}
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(params, "UTF-8");
if (log.isDebugEnabled()) {
log.debug("http client params:" + params.toString());
log.debug("http client url:" + url);
}
log.info("http client url:" + url);
log.info("http client params:" + params.toString());
HttpUriRequest reqMethod = null;
if (METHOD_POST.equalsIgnoreCase("POST")) {
reqMethod = RequestBuilder.post().setUri(url)
// .setCharset(java.nio.charset.Charset.forName("UTF-8"))
// .addParameters(params.toArray(new
// BasicNameValuePair[params.size()]))
.setEntity(urlEncodedFormEntity).build();
} /*
* else if(METHOD_GET.equalsIgnoreCase(method)) { reqMethod =
* RequestBuilder.get().setUri(url)
* .setEntity(urlEncodedFormEntity)
* //.addParameters(params.toArray(new
* BasicNameValuePair[params.size()]))
* .setConfig(requestConfig).build(); }
*/ else {
log.warn("method unknow, return null.");
return null;
}
if (httpclient != null)
response = httpclient.execute(reqMethod);
String string = EntityUtils.toString(response.getEntity(), "UTF-8");
log.info("statusCode: " + response.getStatusLine().getStatusCode());
log.info("resp: " + string);
if (response != null && response.getStatusLine().getStatusCode() == 200)
return string;
else {
if (response != null)
log.warn("http response status error, status{}, return null"
+ response.getStatusLine().getStatusCode());
return null;
}
} catch (Exception e) {
log.error(e.getMessage());
throw e;
} finally {
if (response != null)
response.close();
if (httpclient != null)
httpclient.close();
}
}
/**
* 发送Https 并不校验证书
*
* @param url 地址
* @param json 数据
* @return
* @throws Exception
*/
public static String sendRequestNoCheckCerPostJOSNString ( String url, String json, HttpHost proxy, boolean isProxy ) throws Exception {
int timeout = 60;
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = getHttpClient(timeout, proxy, isProxy);
CloseableHttpResponse response = null;
HttpPost httpPost = new HttpPost(url);
try {
StringEntity entity = new StringEntity(json, "utf-8");// 解决中文乱码问题
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
if (log.isDebugEnabled())
log.debug("executing request :{}" + httpPost.getRequestLine());
HttpUriRequest reqMethod = RequestBuilder.post().setUri(url).setEntity(entity).build();
if (httpclient != null)
response = httpclient.execute(reqMethod);
if (response != null && response.getStatusLine().getStatusCode() == 200)
return EntityUtils.toString(response.getEntity(), "UTF-8");
else {
if (response != null)
log.warn(" status code {} " + response.getStatusLine().getStatusCode());
log.warn(" server error, return null");
return null;
}
} catch (Exception e) {
log.error(e.getMessage());
throw e;
} finally {
if (response != null)
response.close();
if (httpclient != null)
httpclient.close();
}
}
/**
* post 请求 json 数据
*
* @param json JSON
* @param url 地址
* @param timeout 设置超时时间
* @return
* @throws Exception
*/
public static String sendJsonRequestMethod ( String json, String url, int timeout, HttpHost proxy, boolean isProxy ) throws Exception {
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = getHttpClient(timeout, proxy, isProxy);
HttpPost httpPost = new HttpPost(url);
CloseableHttpResponse response = null;
try {
StringEntity entity = new StringEntity(json, "utf-8");// 解决中文乱码问题
entity.setContentType("application/json");
entity.setContentEncoding("UTF-8");
httpPost.setEntity(entity);
if (log.isDebugEnabled())
log.debug("executing request :{}" + httpPost.getRequestLine());
HttpUriRequest reqMethod = RequestBuilder.post().setUri(url).setEntity(entity).build();
if (httpclient != null) {
response = httpclient.execute(reqMethod);
}
if (response != null && response.getStatusLine().getStatusCode() == 200) {
return EntityUtils.toString(response.getEntity(), "UTF-8");
} else {
if (response != null)
log.warn(" server error, return null");
log.warn(" status code {} " + response.getStatusLine().getStatusCode());
return null;
}
} catch (Exception e) {
log.error(e.getMessage());
throw e;
} finally {
if (response != null)
response.close();
if (httpclient != null)
httpclient.close();
}
}
/**
* get请求,支持SSL
*
* @param url 请求地址
* @param headers 请求头信息
* @param params 请求参数
* @param timeOut 超时时间(毫秒):从连接池获取连接的时间,请求时间,响应时间
* @param isStream 是否以流的方式获取响应信息
* @param clientContext Http请求客户端上下文对象,包含Cookie
* @return 响应信息
* @throws Exception
*/
public static String httpGetImage ( String url, JSONObject headers, JSONObject params, Integer timeOut, boolean isStream, HttpClientContext clientContext, HttpHost proxy, boolean isProxy, String imagePath ) throws Exception {
// 构建url
URIBuilder uriBuilder = new URIBuilder(url);
HttpGet httpGet = new HttpGet(url);
// 添加请求参数信息
if (null != params) {
uriBuilder.setParameters(covertParams2NVPS(params));
}
// 创建get请求
// 添加请求头信息
if (null != headers) {
for (Map.Entry<String, Object> entry : headers.entrySet()) {
httpGet.addHeader(entry.getKey(), entry.getValue().toString());
}
}
return getResultImage(httpGet, timeOut, isStream, clientContext, proxy, isProxy, imagePath);
}
private static String getResultImage ( HttpRequestBase httpRequest, Integer timeOut, boolean isStream, HttpClientContext clientContext, HttpHost proxy, boolean isProxy, String imagePath ) throws Exception {
// 响应结果
StringBuilder sb = null;
CloseableHttpResponse response = null;
CloseableHttpClient httpClient = null;
try {
// 获取连接客户端
httpClient = getHttpClient(timeOut, proxy, isProxy);
// 发起请求
if (null != clientContext) {
response = httpClient.execute(httpRequest, clientContext);
} else {
response = httpClient.execute(httpRequest);
}
int respCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
sb = new StringBuilder();
// 如果是以流的形式获取
if (isStream) {
String image_path = imagePath + System.currentTimeMillis() + ".png";
FileUtils.copyToFile(entity.getContent(), new File(image_path));
sb.append(image_path);
} else {
sb.append(EntityUtils.toString(entity, ENCODING));
if (sb.length() < 1) {
sb.append("-1");
}
}
//}
} catch (ConnectionPoolTimeoutException e) {
log.error("从连接池获取连接超时!!!");
e.printStackTrace();
} catch (SocketTimeoutException e) {
log.error("响应超时");
e.printStackTrace();
} catch (ConnectTimeoutException e) {
log.error("请求超时");
e.printStackTrace();
} catch (ClientProtocolException e) {
log.error("http协议错误");
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
log.error("不支持的字符编码");
e.printStackTrace();
} catch (UnsupportedOperationException e) {
log.error("不支持的请求操作");
e.printStackTrace();
} catch (ParseException e) {
log.error("解析错误");
e.printStackTrace();
} catch (IOException e) {
log.error("IO错误");
e.printStackTrace();
} finally {
if (null != response) {
try {
response.close();
} catch (IOException e) {
log.error("关闭响应连接出错");
e.printStackTrace();
}
}
if (httpClient != null) {
httpClient.close();
}
}
return sb == null ? RESULT : ("".equals(sb.toString().trim()) ? "-1" : sb.toString());
}
public static void main ( String[] args ) throws Exception {
HttpClientContext clientContext = HttpClientContext.create();
CookieStore cookieStore = new BasicCookieStore();
/* BasicClientCookie cookie = new BasicClientCookie("Hm_lpvt_de769ee64d6270439549be36ba257ff6", "1553933779");
cookie.setVersion(0);
cookie.setDomain(".12123.com");
cookie.setPath("/");
cookieStore.addCookie(cookie);
*/
clientContext.setCookieStore(cookieStore);
/** 登录 */
JSONObject head = JSONObject.parseObject("{\"User-Agent\":\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36\"}");
/* *//** 验证是否登录 *//*
log.info(httpPost("http://localhost/auth/isLogin", null, null, 6000, false, clientContext));
*//** 退出登录 *//*
log.info(httpPost("http://localhost/auth/logout", null, null, 6000, false, clientContext));*/
}
}
相关推荐
本篇文章将深入探讨如何使用HTTPClient来实现session会话的保持,并在模拟登录后执行后续的操作。 首先,了解HTTP协议的基础知识是非常重要的。HTTP协议是无状态的,这意味着每次请求之间没有任何关联。为了保持...
7. **执行其他需要保持SESSION状态的请求**:现在,当我们创建新的`HttpGet`或`HttpPost`请求时,HttpClient会自动处理Cookie,保持SESSION状态。 ```java HttpGet otherRequest = new HttpGet(...
本文将详细介绍如何使用HttpClient在Android中实现网络请求,并通过Cookie来维持会话状态,以便进行持久化的用户登录。 首先,我们需要了解HttpClient的基本用法。HttpClient是一个强大的HTTP客户端库,它允许...
4. **执行请求**:通过HttpClient对象发送请求,并获取HttpResponse。 5. **处理响应**:从HttpResponse中读取状态码、头信息和响应体,进行业务逻辑处理。 6. **释放资源**:完成操作后,记得关闭连接,释放资源...
此外,HttpClient 4.1.2还提供了对cookies的处理,通过`CookieStore`和`CookieSpec`接口,可以实现cookies的存储、读取和管理,从而保持会话状态。 在错误处理上,HttpClient 4.1.2引入了重试策略,当网络不稳定...
5. **Cookie管理**:HttpClient支持`CookieSpecs`和`CookieStore`,用于处理服务器返回的Cookie,实现会话保持。 6. **请求和响应处理**:HttpClient允许自定义请求实体和响应处理器,支持JSON、XML等数据格式的...
4. **Cookie管理**:HttpClient内置了Cookie管理器,可以处理服务器返回的Cookie,保持会话状态,支持标准的Cookie规范。 5. **SSL/TLS支持**:HttpClient支持安全的HTTPS通信,可以自定义SSL上下文,处理证书、...
8. **Cookie Management**:对于处理需要维持会话的HTTP请求,HttpClient支持Cookie管理,可以设置`CookieStore`和`CookiePolicy`。 9. **Authentication**:HttpClient支持多种认证机制,包括基本认证、NTLM、...
9. **Cookie管理**:HttpClient4.2.1支持Cookie规范,通过`CookieStore`和`CookieSpec`处理服务器返回的Cookie,实现会话保持。 10. **性能优化**:HttpClient4.2.1进行了多项性能优化,例如支持GZIP压缩、减少内存...
5. **Cookie管理**:HttpClient可以自动处理服务器返回的cookies,保持会话状态,方便处理登录和其他需要保持状态的场景。 6. **异步请求**:虽然HttpClient主要是同步操作,但通过配合线程或者回调机制,可以实现...
5. **支持Cookie管理**:HttpClient能够自动处理服务器返回的Cookie,保持会话状态。 6. **代理设置**:支持配置HTTP代理和SOCKS代理,适应不同网络环境的需求。 7. **安全通信**:通过SSL/TLS协议支持HTTPS,确保...
- **使用Cookie管理器**:对于处理需要保持会话的请求,可以使用`CookieStore`和`CookiePolicy`来管理Cookie。 - **处理重定向**:HttpClient支持自动处理重定向,但也可以通过配置关闭此功能,自定义重定向策略。...
- **Cookie管理**:处理服务器返回的Cookie,保持会话状态。 - **SSL/TLS支持**:配置SSL上下文,处理HTTPS请求。 - **重试策略**:当请求失败时,自动按照预设策略进行重试。 - **线程安全**:确保封装的HttpClient...
9. **Cookie管理**:HttpClient可以管理Cookie,处理会话保持和跨域问题。 在实际应用中,为了使用HttpClient,你需要将zip文件中的jar包解压后添加到项目的类路径中。同时,根据项目需求,可能还需要添加其他依赖...
这在需要保持会话状态或处理服务器的Set-Cookie响应头时很有用。 10. **错误处理**:在处理响应时,需要检查状态码以确定请求是否成功。通常,2xx系列的状态码表示成功,而4xx和5xx系列则表示客户端或服务器端的...
- 如果登录成功,服务器可能会返回一个会话cookie,HttpClient需要保存这个cookie以保持登录状态。 3. **HttpClient API使用** - `HttpClient`实例化:创建一个`HttpClient`对象,可以设置连接超时、重试策略等...
- **请求和响应处理**:HttpClient提供了`HttpRequestExecutor`和`HttpResponseHandler`接口,用于定制请求执行逻辑和响应处理方式。 - **身份验证与安全**:HttpClient支持多种认证机制,如Basic Auth、Digest ...