Clientbuilder.sslsocketfactory(sslsocketfactory) not supported on jdk 9+

When using the ClientBuilder class in JDK 9 and above, the method sslSocketFactory() is not supported directly. This method allowed you to set a custom SSL socket factory for making HTTPS connections.

Instead, starting from JDK 9, the preferred way to configure SSL/TLS settings for HTTP clients is through the HttpClient class introduced in the java.net.http package. The HttpClient class provides a more modern and flexible API for making HTTP requests.

Here is an example on how to configure SSL/TLS settings using the HttpClient class in JDK 9+:

    
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;

// Create a custom SSLContext with desired SSL/TLS configuration
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, null, null);

// Create custom SSLParameters
SSLParameters sslParameters = new SSLParameters();
sslParameters.setProtocols(new String[] { "TLSv1.2" }); // Set desired protocols

// Create HttpClient with desired SSL/TLS settings
HttpClient httpClient = HttpClient.newBuilder()
        .sslContext(sslContext)
        .sslParameters(sslParameters)
        .build();

// Create HttpRequest and send the request
HttpRequest request = HttpRequest.newBuilder()
        .uri(new URI("https://example.com"))
        .build();
        
HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());

// Process the response
System.out.println(response.body());
    
  

In this example, we first create a custom SSLContext and SSLParameters with the desired SSL/TLS configuration. Then, we use the HttpClient.newBuilder() method to create a new HttpClient instance, and use the sslContext() and sslParameters() methods to set the custom SSLContext and SSLParameters respectively. Finally, we make an HTTP request and process the response.

Similar post

Leave a comment