Search code examples
javaspring-bootssl-certificatehttpclientapache-httpclient-5.x

HttpClients.custom().setSSLSocketFactory() method not found


I have used

implementation group: 'org.apache.httpcomponents.client5', name: 'httpclient5', version: '5.2'

setSSLSocketFactory() method doesn't exist for this dependency


Solution

  • It seems that the SSLSocketFactory should now be set on the ConnectionManagerBuilder

    See the official documentation here with an example: Migration to Apache HttpClient 5.x classic APIs

    Sample code:

    PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create()
          .setSSLSocketFactory(SSLConnectionSocketFactoryBuilder.create()
                  .setSslContext(SSLContexts.createSystemDefault())
                  .setTlsVersions(TLS.V_1_3)
                  .build())
          .setDefaultSocketConfig(SocketConfig.custom()
                  .setSoTimeout(Timeout.ofMinutes(1))
                  .build())
          .setPoolConcurrencyPolicy(PoolConcurrencyPolicy.STRICT)
          .setConnPoolPolicy(PoolReusePolicy.LIFO)
          .setDefaultConnectionConfig(ConnectionConfig.custom()
                  .setSocketTimeout(Timeout.ofMinutes(1))
                  .setConnectTimeout(Timeout.ofMinutes(1))
                  .setTimeToLive(TimeValue.ofMinutes(10))
                  .build())
          .build();
    HttpClient httpClient = HttpClientBuilder
                            .create()
                            .setConnectionManager(connectionManager)
                            .build();
    

    I did it and it is working fine