I need to add these 3 pem files (ca.pem
, key.pem
, and cert.pem
) to my http client in order to access a client's service.
How do I get these to work with my existing httpclient? Any help will be appreciated.
Thank you.
File caFile = new File(getClass().getResource("/certs/ca.pem").getPath());
File keyFile = new File(getClass().getResource("/certs/key.pem").getPath());
File certFile = new File(getClass().getResource("/certs/cert.pem").getPath());
SSLContext sslContext = new SSLContextBuilder()
.loadTrustMaterial(null, ( certificate, authType ) -> true).build();
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLContext(sslContext)
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.addInterceptorFirst((HttpRequestInterceptor) ( httpRequest, httpContext ) -> {
httpRequest.setHeader("Content-Type", "application/xml");
})
.build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);