3

I have upgraded from apache http client 5.3.x to 5.4.x.

The below code works fine

var sslContext = SSLContexts.custom() .loadTrustMaterial(null, TrustAllStrategy.INSTANCE) .build(); SSLConnectionSocketFactory sslConnSocketFactory = SSLConnectionSocketFactoryBuilder.create() .setHostnameVerifier(NoopHostnameVerifier.INSTANCE) .setSslContext(sslContext) .build(); var connectionManager = PoolingHttpClientConnectionManagerBuilder.create() .setSSLSocketFactory(sslConnSocketFactory) .build(); CloseableHttpClient client = HttpClientBuilder.create() .setConnectionManager(connectionManager) .build(); HttpGet httpGet = new HttpGet("https://mms.nw.ru/"); HttpHost host = RoutingSupport.determineHost(httpGet); ClassicHttpResponse response = client.executeOpen(host, httpGet, null); Assertions.assertEquals(200, response.getCode()); 

But the SSLConnectionSocketFactory class and setSSLSocketFactory method of PoolingHttpClientConnectionManagerBuilder became deprecated.

I have changed the code to

var sslContext = SSLContexts.custom() .loadTrustMaterial(null, TrustAllStrategy.INSTANCE) .build(); var tlsStrategy = ClientTlsStrategyBuilder.create() .setSslContext(sslContext) .setHostnameVerifier(NoopHostnameVerifier.INSTANCE) .build(); var connectionManager = PoolingHttpClientConnectionManagerBuilder.create() .setTlsSocketStrategy((TlsSocketStrategy) tlsStrategy) .build(); CloseableHttpClient client = HttpClientBuilder.create() .setConnectionManager(connectionManager) .build(); HttpGet httpGet = new HttpGet("https://mms.nw.ru/"); HttpHost host = RoutingSupport.determineHost(httpGet); ClassicHttpResponse response = client.executeOpen(host, httpGet, null); Assertions.assertEquals(200, response.getCode()); 

It throws javax.net.ssl.SSLHandshakeException: No name matching mms.nw.ru found cause java.security.cert.CertificateException: No name matching mms.nw.ru found.

What is wrong ?

2
  • This question is similar to: HttpClients.custom().setSSLSocketFactory() method not found. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Dec 6, 2024 at 1:52
  • @aled My question was not about missing but replacement deprecation of ConnectionManagerBuilder.setSSLSocketFactory method. Thanks for suggestion. Commented Dec 6, 2024 at 12:54

1 Answer 1

5

As of version 5.4 HttpClient makes use of built-in hostname verification provided by JSSE. In order to disable hostname verification completely initialize the TLS strategy in your code the following way

var tlsStrategy = new DefaultClientTlsStrategy( sslContext, HostnameVerificationPolicy.CLIENT, NoopHostnameVerifier.INSTANCE); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.