分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

httpclient4使用说明

发布时间:2023-09-06 01:49责任编辑:郭大石关键词:http
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<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> 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 ???????????} ???????} ???}}

httpclient4使用说明

原文地址:http://blog.51cto.com/chend/2097087

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved