use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project camel by apache.
the class HttpEndpointURLTest method testConnectionManagerFromHttpUri.
@Test
public void testConnectionManagerFromHttpUri() throws Exception {
HttpEndpoint http1 = context.getEndpoint("http4://www.google.com?maxTotalConnections=40&connectionsPerRoute=5", HttpEndpoint.class);
HttpClientConnectionManager connectionManager = http1.getClientConnectionManager();
assertTrue("Get a wrong type of connection manager", connectionManager instanceof PoolingHttpClientConnectionManager);
@SuppressWarnings("resource") PoolingHttpClientConnectionManager poolManager = (PoolingHttpClientConnectionManager) connectionManager;
assertEquals("Get a wrong setting of maxTotalConnections", 40, poolManager.getMaxTotal());
assertEquals("Get a wrong setting of connectionsPerRoute", 5, poolManager.getDefaultMaxPerRoute());
}
use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project quickutil by quickutil.
the class HttpUtil method initHttpsClientMananger.
/**
* 生成https连接管理器
*
* @param clientCer-客户端证书
* @param clientPW-客户端证书密钥
* @param serverCer-服务端证书
* @param serverPW-服务端证书密钥
* @return
*/
public static HttpClientConnectionManager initHttpsClientMananger(InputStream clientCer, String clientPW, InputStream serverCer, String serverPW) {
try {
KeyManager[] keysManagers = null;
TrustManager[] trustManagers = null;
// 验证客户端证书
if (clientCer != null) {
KeyStore ks = KeyStore.getInstance("pkcs12");
ks.load(clientCer, clientPW.toCharArray());
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(ks, clientPW.toCharArray());
keysManagers = keyManagerFactory.getKeyManagers();
}
// 验证服务端证书
if (serverCer != null) {
KeyStore ks2 = KeyStore.getInstance("pkcs12");
ks2.load(serverCer, serverPW.toCharArray());
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(ks2);
trustManagers = trustManagerFactory.getTrustManagers();
} else {
trustManagers = new TrustManager[] { tm };
}
// 生成ssl参数
SSLContext context = SSLContext.getInstance("TLS");
context.init(keysManagers, trustManagers, null);
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(context);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", socketFactory).build();
return new PoolingHttpClientConnectionManager(socketFactoryRegistry);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project questdb by bluestreak01.
the class HttpTestUtils method createHttpClient_AcceptsUntrustedCerts.
private static HttpClientBuilder createHttpClient_AcceptsUntrustedCerts() throws Exception {
HttpClientBuilder b = HttpClientBuilder.create();
// setup a Trust Strategy that allows all certificates.
//
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (arg0, arg1) -> true).build();
b.setSSLContext(sslContext);
// here's the special part:
// -- need to create an SSL Socket Factory, to use our weakened "trust strategy";
// -- and create a Registry, to register it.
//
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, (s, sslSession) -> true);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory).build();
// now, we create connection-manager using our Registry.
// -- allows multi-threaded use
b.setConnectionManager(new PoolingHttpClientConnectionManager(socketFactoryRegistry));
return b;
}
use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project pact-jvm by DiUS.
the class InsecureHttpsRequest method setupInsecureSSL.
private void setupInsecureSSL() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
HttpClientBuilder b = HttpClientBuilder.create();
// setup a Trust Strategy that allows all certificates.
//
TrustStrategy trustStrategy = (chain, authType) -> true;
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, trustStrategy).build();
b.setSSLContext(sslContext);
// don't check Hostnames, either.
// -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), if you don't want to weaken
HostnameVerifier hostnameVerifier = new NoopHostnameVerifier();
// here's the special part:
// -- need to create an SSL Socket Factory, to use our weakened "trust strategy";
// -- and create a Registry, to register it.
//
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory).build();
// now, we create connection-manager using our Registry.
// -- allows multi-threaded use
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
b.setConnectionManager(connMgr);
// finally, build the HttpClient;
// -- done!
this.httpclient = b.build();
}
use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project bitsquare by bitsquare.
the class HttpClient method requestWithGETProxy.
/**
* Make an HTTP Get request routed over socks5 proxy.
*/
private String requestWithGETProxy(String param, Socks5Proxy socks5Proxy, @Nullable String headerKey, @Nullable String headerValue) throws IOException, HttpException {
log.debug("requestWithGETProxy param=" + param);
// This code is adapted from:
// http://stackoverflow.com/a/25203021/5616248
// Register our own SocketFactories to override createSocket() and connectSocket().
// connectSocket does NOT resolve hostname before passing it to proxy.
Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create().register("http", new SocksConnectionSocketFactory()).register("https", new SocksSSLConnectionSocketFactory(SSLContexts.createSystemDefault())).build();
// Use FakeDNSResolver if not resolving DNS locally.
// This prevents a local DNS lookup (which would be ignored anyway)
PoolingHttpClientConnectionManager cm = socks5Proxy.resolveAddrLocally() ? new PoolingHttpClientConnectionManager(reg) : new PoolingHttpClientConnectionManager(reg, new FakeDnsResolver());
try (CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build()) {
InetSocketAddress socksaddr = new InetSocketAddress(socks5Proxy.getInetAddress(), socks5Proxy.getPort());
// remove me: Use this to test with system-wide Tor proxy, or change port for another proxy.
// InetSocketAddress socksaddr = new InetSocketAddress("127.0.0.1", 9050);
HttpClientContext context = HttpClientContext.create();
context.setAttribute("socks.address", socksaddr);
HttpGet request = new HttpGet(baseUrl + param);
if (headerKey != null && headerValue != null)
request.setHeader(headerKey, headerValue);
log.debug("Executing request " + request + " proxy: " + socksaddr);
try (CloseableHttpResponse response = httpclient.execute(request, context)) {
return convertInputStreamToString(response.getEntity().getContent());
}
} catch (Throwable t) {
log.debug("Error at requestWithGETProxy: " + t.getMessage());
throw new IOException(t);
}
}
Aggregations