Skip to content

Instantly share code, notes, and snippets.

@tmarcus87
Created June 3, 2014 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmarcus87/e7da2f90fd962cedc780 to your computer and use it in GitHub Desktop.
Save tmarcus87/e7da2f90fd962cedc780 to your computer and use it in GitHub Desktop.
public HttpClientFactoryV43 {
private BasicHttpClientConnectionManager connectionManager;
public HttpClientFactoryV43() {
try {
SSLContext sslContext = new SSLContextBuilder()
.loadTrustMaterial(
null,
new TrustSelfSignedStrategy())
.loadTrustMaterial(
null,
new TrustStrategy() {
@Override public boolean isTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
return true;
}
})
.build();
SSLConnectionSocketFactory sslConnectionSocketFactory =
new SSLConnectionSocketFactory(
sslContext,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Registry<ConnectionSocketFactory> registry =
RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslConnectionSocketFactory)
.build();
connectionManager = new BasicHttpClientConnectionManager(registry);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (KeyManagementException e) {
throw new RuntimeException(e);
} catch (KeyStoreException e) {
throw new RuntimeException(e);
}
}
public HttpClient insecureClient() {
RequestConfig config =
RequestConfig.custom()
.setSocketTimeout(1000)
.setConnectTimeout(1000)
.setConnectionRequestTimeout(1000)
.setStaleConnectionCheckEnabled(true)
.build();
return HttpClients.custom()
.setDefaultRequestConfig(config)
.setMaxConnPerRoute(100)
.setMaxConnTotal(100)
.disableAutomaticRetries()
.disableConnectionState()
.disableContentCompression()
.disableRedirectHandling()
.setConnectionManager(connectionManager)
.build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment