public HttpClientGenerator() { Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.INSTANCE) .register("https", SSLConnectionSocketFactory.getSocketFactory()) .build(); connectionManager = new PoolingHttpClientConnectionManager(reg); connectionManager.setDefaultMaxPerRoute(100); }
private PoolingHttpClientConnectionManager createSystemSslCM() { ConnectionSocketFactory socketFactory = SSLConnectionSocketFactory.getSystemSocketFactory(); Registry registry = RegistryBuilder.<ConnectionSocketFactory>create() .register(PROTOCOL, socketFactory) .build(); return new PoolingHttpClientConnectionManager(registry); }
/** * Sets the timeouts of the HTTP client. * * @param connectionTimeout timeout until connection established in milliseconds. Zero means no * timeout. * @param socketTimeout timeout for waiting for data in milliseconds. Zero means no timeout. * @param maxRequests maximum number of connections to a particuar host */ public static void setParams(int connectionTimeout, int socketTimeout, int maxRequests) { PrudentHttpEntityResolver.maxRequests = maxRequests; PoolingHttpClientConnectionManager phcConnMgr; Registry<ConnectionSocketFactory> registry = // RegistryBuilder.<ConnectionSocketFactory>create() // .register("http", PlainConnectionSocketFactory.getSocketFactory()) // .register("https", SSLConnectionSocketFactory.getSocketFactory()) // .build(); HttpClientBuilder builder = HttpClients.custom(); builder.setRedirectStrategy(new LaxRedirectStrategy()); builder.setMaxConnPerRoute(maxRequests); builder.setMaxConnTotal(200); if ("true".equals(System.getProperty("nu.validator.xml.promiscuous-ssl", "false"))) { // try { SSLContext promiscuousSSLContext = new SSLContextBuilder() // .loadTrustMaterial( null, new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { return true; } }) .build(); builder.setSslcontext(promiscuousSSLContext); HostnameVerifier verifier = // SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER; SSLConnectionSocketFactory promiscuousSSLConnSocketFactory = // new SSLConnectionSocketFactory(promiscuousSSLContext, verifier); registry = RegistryBuilder.<ConnectionSocketFactory>create() // .register("https", promiscuousSSLConnSocketFactory) // .register("http", PlainConnectionSocketFactory.getSocketFactory()) // .build(); } catch (KeyManagementException | KeyStoreException | NoSuchAlgorithmException | NumberFormatException e) { e.printStackTrace(); } } phcConnMgr = new PoolingHttpClientConnectionManager(registry); phcConnMgr.setDefaultMaxPerRoute(maxRequests); phcConnMgr.setMaxTotal(200); builder.setConnectionManager(phcConnMgr); RequestConfig.Builder config = RequestConfig.custom(); config.setCircularRedirectsAllowed(true); config.setMaxRedirects(20); // Gecko default config.setConnectTimeout(connectionTimeout); config.setCookieSpec(CookieSpecs.BEST_MATCH); config.setSocketTimeout(socketTimeout); client = builder.setDefaultRequestConfig(config.build()).build(); }
/** * 个性化配置连接管理器 * * @param maxConnectionsPerHost 设置默认的连接到每个主机的最大连接数 * @param maxTotalConnections 设置整个管理连接器的最大连接数 * @param conTimeOutMs 连接超时 * @param soTimeOutMs socket超时 * @param routeCfgList 特殊路由配置列表,若无请填null * @param proxy 代理设置,若无请填null */ public QHttpClient( int maxConnectionsPerHost, int maxTotalConnections, int conTimeOutMs, int soTimeOutMs, List<RouteCfg> routeCfgList, HttpHost proxy) { // 使用默认的 socket factories 注册 "http" & "https" protocol scheme Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.INSTANCE) .register("https", SSLConnectionSocketFactory.getSocketFactory()) .build(); PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(reg); SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(soTimeOutMs).build(); connectionManager.setDefaultMaxPerRoute(maxConnectionsPerHost); connectionManager.setMaxTotal(maxTotalConnections); connectionManager.setDefaultSocketConfig(socketConfig); // 对特定路由修改最大连接数 if (null != routeCfgList) { for (RouteCfg routeCfg : routeCfgList) { HttpHost localhost = new HttpHost(routeCfg.getHost(), routeCfg.getPort()); connectionManager.setMaxPerRoute(new HttpRoute(localhost), routeCfg.getMaxConnetions()); } } // 设置代理 DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy); httpClient = HttpClients.custom() .setConnectionManager(connectionManager) .setRoutePlanner(routePlanner) .build(); }
private static Registry<ConnectionSocketFactory> getDefaultRegistry() { return RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", SSLConnectionSocketFactory.getSocketFactory()) .build(); }
public static CloseableHttpClient buildHttpClient() throws Exception { TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws CertificateException {} @Override public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws CertificateException {} } }; SSLContext sslcontext = SSLContext.getInstance("SSL"); sslcontext.init(null, trustAllCerts, new java.security.SecureRandom()); // Allow TLSv1 protocol only SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( sslcontext, new String[] {"TLSv1"}, null, SSLConnectionSocketFactory.getDefaultHostnameVerifier()); CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); return httpclient; }