httpClient4.5.2工具类总结
使用背景:
因项目使用非结构化存储,http相关jar包统一升级到httpClient4.5.2,查阅相关文档总结如下,以咨分享,望不吝指教。
依赖jar包
httpclient-4.5.2.jar、httpcore-4.4.4.jar、sl4j.jar
HttpClientUtil.java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InterruptedIOException;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager; import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.NoHttpResponseException;
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.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.ConnectTimeoutException;
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.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
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.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; /**
* 参考:https://www.cnblogs.com/luken2017/p/6386055.html
* https://www.2cto.com/net/201709/681448.html
* https://www.cnblogs.com/likaitai/p/5431246.html
*/
public class HttpClientUtil { private static final Logger LOG = LoggerFactory.getLogger(HttpClientUtil.class); /** 连接池 */
private static PoolingHttpClientConnectionManager connManager = null; // 代理信息
private static final String PROXY_IP = "10.5.3.9";
private static final int PROXY_PORT = 80; private static int maxTotalPool = 1000;
private static int maxConPerRoute = 20;
private static int allTimeout = 15000; private static HashMap<String, HttpClientUtil> map = new HashMap<>(); private static Object object = new Object(); private String url = null; private CloseableHttpClient httpClient = null; public static HttpClientUtil getInstance(String strUrl) {
HttpClientUtil instance = null;
if ((instance = map.get(strUrl)) == null) {
synchronized (object) {
initConnManager(); instance = new HttpClientUtil();
instance.httpClient = getHttpClient(false, allTimeout);
instance.url = strUrl;
map.put(strUrl, instance);
}
}
return instance;
} public static HttpClientUtil getProxyInstance(String strUrl) {
HttpClientUtil instance = null;
if ((instance = map.get(strUrl)) == null) {
synchronized (object) {
initConnManager(); instance = new HttpClientUtil();
instance.httpClient = getHttpClient(true, allTimeout);
instance.url = strUrl;
map.put(strUrl, instance);
}
}
return instance;
} public static void initConnManager() {
if (connManager == null) {
// 创建ssl安全访问连接
// 获取创建ssl上下文对象
try {
SSLContext sslContext = getSSLContext(true, null, null);
// 注册
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", new SSLConnectionSocketFactory(sslContext)).build();
// ssl注册到连接池
connManager = new PoolingHttpClientConnectionManager(registry);
connManager.setMaxTotal(maxTotalPool); // 连接池最大连接数
connManager.setDefaultMaxPerRoute(maxConPerRoute); // 每个路由最大连接数 } catch (Exception e) {
LOG.error(e.getMessage(), e);
}
}
} private static SSLContext getSSLContext(boolean isDeceive, File creFile, String crePwd)
throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, CertificateException,
IOException {
SSLContext sslContext = null; if (isDeceive) {
sslContext = SSLContext.getInstance("SSLv3");
// 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
X509TrustManager x509TrustManager = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
// Do nothing
} @Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
// Do nothing
} @Override
public X509Certificate[] getAcceptedIssuers() {
// Do nothing
return new X509Certificate[0];
}
};
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;
} public static CloseableHttpClient getHttpClient(boolean startProxy, int timeout) {
return getHttpClient(startProxy, timeout, timeout, timeout);
} private static CloseableHttpClient getHttpClient(boolean startProxy, int connectionRquestTimeout, int connectTimeout, int socketTimeout) {
// 配置请求参数
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(connectionRquestTimeout)
.setConnectTimeout(connectTimeout)
.setSocketTimeout(socketTimeout)
.build(); // 配置超时回调机制
HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() { @Override
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();
// 如果请求是幂等的,就再次尝试
return !(request instanceof HttpEntityEnclosingRequest) ? true : false;
} }; HttpClientBuilder httpClientBuilder = HttpClients.custom(); httpClientBuilder.setConnectionManager(connManager).setDefaultRequestConfig(requestConfig)
.setRetryHandler(retryHandler); if (startProxy) {
HttpHost proxy = new HttpHost(PROXY_IP, PROXY_PORT);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
httpClientBuilder.setRoutePlanner(routePlanner);
} return httpClientBuilder.build();
} public String postJson(String json) {
HttpPost post = new HttpPost(url);
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
post.setEntity(entity);
String responseContent = null;
CloseableHttpResponse response = null;
try {
response = httpClient.execute(post);
int status = response.getStatusLine().getStatusCode();
printStatus(status);
if (status == HttpStatus.SC_OK) {
responseContent = EntityUtils.toString(response.getEntity(), Consts.UTF_8.name());
} } catch (IOException e) {
LOG.error(e.getMessage(), e);
} finally {
post.abort();
post.releaseConnection();
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
response.close();
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}
}
return responseContent;
} /**
* 发送 GET 请求(HTTP),不带输入数据
*
* @param url
* @return
*/
public String doGet(String url) {
return doGet(new HashMap<String, Object>());
} /**
* 发送 GET 请求(HTTP),K-V形式
*
* @param url
* @param params
* @return
*/
public String doGet(Map<String, Object> params) {
String apiUrl = url;
StringBuilder param = new StringBuilder();
int i = 0;
for (Map.Entry<String, Object> entry : params.entrySet()) {
if (i == 0) {
param.append("?");
} else {
param.append("&");
}
param.append(entry.getKey()).append("=").append(entry.getValue());
i++;
} apiUrl += param;
String result = null;
CloseableHttpResponse response = null;
HttpGet httpGet = null;
try {
httpGet = new HttpGet(apiUrl);
response = httpClient.execute(httpGet);
int status = response.getStatusLine().getStatusCode();
printStatus(status);
if (status == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(response.getEntity(), Consts.UTF_8.name());
}
}
} catch (IOException e) {
LOG.error(e.getMessage(), e);
} finally {
httpGet.abort();
httpGet.releaseConnection();
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
response.close();
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}
} return result;
} /**
* 发送 POST 请求(HTTP),K-V形式
*
* @param url
* 接口URL
* @param params
* 参数map
* @return
*/
public String doPost(Map<String, String> params) {
String result = null;
HttpPost httpPost = new HttpPost(url);
CloseableHttpResponse response = null;
try {
List<NameValuePair> pairList = new ArrayList<>(params.size());
for (Map.Entry<String, String> entry : params.entrySet()) {
NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue());
pairList.add(pair);
}
httpPost.setEntity(new UrlEncodedFormEntity(pairList, Consts.UTF_8.name()));
response = httpClient.execute(httpPost);
int status = response.getStatusLine().getStatusCode();
printStatus(status);
if (status == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(response.getEntity(), Consts.UTF_8.name());
}
} } catch (IOException e) {
LOG.error(e.getMessage(), e);
} finally {
httpPost.abort();
httpPost.releaseConnection();
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
response.close();
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}
}
return result;
} private void printStatus(int status) {
LOG.info("返回状态:{}", status);
}
}
httpClient4.5.2工具类总结的更多相关文章
- httpclient4.3 工具类
httpclient4.3 java工具类. .. .因项目须要开发了一个工具类.正经常常使用的httpclient 请求操作应该都够用了 工具类下载地址:http://download.csdn. ...
- 基于HttpClient4.5.1实现Http访问工具类
本工具类基于httpclient4.5.1实现 <dependency> <groupId>org.apache.httpcomponents</groupId> ...
- HttpClient4.5 SSL访问工具类
要从网上找一个HttpClient SSL访问工具类太难了,原因是HttpClient版本太多了,稍有差别就不能用,最后笔者干脆自己封装了一个访问HTTPS并绕过证书工具类. 主要是基于新版本Http ...
- 基于HttpClient4.5.2实现的HttpClient工具类
1.maven依赖: <dependency> <groupId>org.apache.commons</groupId> <artifactId>co ...
- java http工具类和HttpUrlConnection上传文件分析
利用java中的HttpUrlConnection上传文件,我们其实只要知道Http协议上传文件的标准格式.那么就可以用任何一门语言来模拟浏览器上传文件.下面有几篇文章从http协议入手介绍了java ...
- 验证工具类 - ValidateUtils.java
验证工具类,提供验证email格式.是否ipv4.是否ipv6.是否中文.是否数字.正则表达式验证的方法. 源码如下:(点击下载 - ValidateUtils.java .commons-lang- ...
- Jsoup请求http或https返回json字符串工具类
Jsoup请求http或https返回json字符串工具类 所需要的jar包如下: jsoup-1.8.1.jar 依赖jar包如下: httpclient-4.5.4.jar; httpclient ...
- Java基础Map接口+Collections工具类
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- Android—关于自定义对话框的工具类
开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...
随机推荐
- linux下面实时查看进程,内存以及cpu使用情况使用命令
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器 可以直接使用top命令查看整体情况,如图: 但是这样虽然看的东西多,但是闲的比较 ...
- conda查看某个安装包的依赖项
查看某个安装包XXX的依赖项的conda指令为: conda search XXX -info 比如XXX为pytorch0.3.1,就会有如下输出: pytorch 0.3.1 py36hfbe70 ...
- C++类的默认成员函数
成员函数隐含this指针参数: 每成员函数一个隐式的指针形参(构造函数除外): 对象在调用成员函数时,编译器会将对象的地址传递给this指针: 1.构造函数(需用一个公有成员函数对私有的成员变量进行初 ...
- js关闭当前页面(窗口)的几种方式
1. 不带任何提示关闭窗口的js代码 代码如下: <a href="javascript:window.opener=null;window.open('','_self');win ...
- CppCheck介绍与使用
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u011012932/article/details/52778149 简述 Cppcheck 是一种 ...
- HOG + SVM(行人检测, opencv实现)
HOG+SVM流程 1.提取HOG特征 灰度化 + Gamma变换(进行根号求解) 计算梯度map(计算梯度) 图像划分成小的cell,统计每个cell梯度直方图 多个cell组成一个block, 特 ...
- Jenkins发布
右键查看图片显示全图
- 如何使用Loadrunner Controller 监控服务器的系统资源
(1)保证装有loadrunner Controller的控制机和被监控的目标机(服务器)之间能够ping通,在同一个网段内,保证两台机器用administrator登陆. (2)Win + R, s ...
- 基于 Linux Bridge 的 Neutron 多平面网络实现原理
目录 文章目录 目录 前言 前文列表 多平面网络 Local(本地网络) Flat(扁平网络) 配置 Flat 网络 VLAN 配置 VLAN 网络 VxLAN 配置 VxLAN 网络 GRE 前言 ...
- 阶段3 2.Spring_07.银行转账案例_3 分析事务的问题并编写ConnectionUtils
不是没有事务造成的 这样相当于有四个connection 每一个都有自己独立的事物 每一个自己成功就提交事务. 已经提交的就执行结束.没有提交的就报异常 让这些操作使用同一个connection 事物 ...