java httpclient4_httpclient4使用说明

该博客详细介绍了如何使用HTTPClient4构建HTTP客户端,并着重讲解了如何配置SSL证书验证,包括创建信任所有证书的SSLContext和设置PoolingHttpClientConnectionManager。此外,还提供了IdleConnectionMonitorThread来定时清理连接池中失效的连接,确保连接管理的有效性。
摘要由CSDN通过智能技术生成

httpclient4使用说明

public class HttpClientObject {

public org.apache.http.client.HttpClient httpClient4New;// = new HttpClient();

public static HttpClient buildHttpClient() {

// builder.setProxy(new HttpHost("127.0.0.1",8888));

return builder.build();

}

//为HttpClient增加跳过SSL证书验证

private static PoolingHttpClientConnectionManager newConnectionManager() {

SSLContext sslcontext = null;

try {

sslcontext = SSLContext.getInstance("TLS");

X509TrustManager tm = new X509TrustManager() {

public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {

}

public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {

}

public X509Certificate[] getAcceptedIssuers() {

return null;

}

};

sslcontext.init(null, new TrustManager[] { tm }, null);

Registry socketFactoryRegistry = RegistryBuilder. create()

.register("http", PlainConnectionSocketFactory.INSTANCE)

.register("https", new SSLConnectionSocketFactory(sslcontext, NoopHostnameVerifier.INSTANCE))

.build();

SocketConfig config = SocketConfig.custom().setSoTimeout(1000 * 60).build();

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);

cm.setMaxTotal(20000);

cm.setDefaultMaxPerRoute(20000);

cm.setDefaultSocketConfig(config);

return cm;

}

catch (Exception e1) {

e1.printStackTrace();

}

return null;

}

private static PoolingHttpClientConnectionManager poolCm = null;

static {

poolCm = newConnectionManager();

new IdleConnectionMonitorThread(poolCm).start();

}

private static final RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectTimeout(1000 * 10)

.setConnectionRequestTimeout(1000 * 10).setSocketTimeout(1000 * 60).build();

private static final HttpClientBuilder builder = HttpClients.custom().setConnectionManager(poolCm)

.setDefaultRequestConfig(defaultRequestConfig);

/**

* 定时清理连接池中失效的连接

*/

private static class IdleConnectionMonitorThread extends Thread {

private final HttpClientConnectionManager connMgr;

private volatile boolean shutdown;

public IdleConnectionMonitorThread(HttpClientConnectionManager connMgr) {

super();

this.connMgr = connMgr;

this.setDaemon(true);

}

@Override

public void run() {

try {

while (!shutdown) {

synchronized (this) {

wait(5000);

// Close expired connections

connMgr.closeExpiredConnections();

// Optionally, close connections

// that have been idle longer than 30 sec

connMgr.closeIdleConnections(30, TimeUnit.SECONDS);

}

}

}

catch (InterruptedException ex) {

// terminate

}

}

}

}

©著作权归作者所有:来自51CTO博客作者呜哈哈666888的原创作品,如需转载,请注明出处,否则将追究法律责任

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值