Home » Java » java – How to use Apache HttpClient Mutual SSL with SSL Proxy-Exceptionshub

java – How to use Apache HttpClient Mutual SSL with SSL Proxy-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have an Apache HttpClient (v4.5.11) which I have set up for mutual SSL as per the code below. This works perfectly, however I need to access this connection through an SSL proxy. I have tried adding the proxy config in the code below (commented) but I cannot get it to work. When making the request I always get the response:

javax.net.ssl.SSLException: Unsupported or unrecognized SSL message

How can I use the Apache client with an SSL proxy?

var keystore = KeyStore.getInstance("JKS");
keystore.load(
    new ByteArrayInputStream(Files.readAllBytes(Paths.get(keystorePath))),
    keystorePassword.toCharArray());
var contextBuilder = SSLContexts.custom();
contextBuilder.loadKeyMaterial(keystore, keystorePassword.toCharArray());
var sslContext = contextBuilder.build();
var socketFactory =
    new SSLConnectionSocketFactory(sslContext, new DefaultHostnameVerifier());
var builder = HttpClientBuilder.create().setSSLSocketFactory(socketFactory);
var poolingHttpClientConnectionManager =
    new PoolingHttpClientConnectionManager(
        RegistryBuilder.<ConnectionSocketFactory>create().
            register("https", socketFactory).
            build());
poolingHttpClientConnectionManager.setMaxTotal(100);
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(100);
builder.setConnectionManager(poolingHttpClientConnectionManager);
//builder.setProxy(new HttpHost("myproxy.com", 443, "https"));
var client = builder.build();
How to&Answers: