javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure in java 17

165 Views Asked by At

I'm trying to send get reuest to the server.But i'm always getting "javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure" .I'm using openjdk-17.0.1 .

package org.example;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLContext;
import java.io.FileInputStream;
import java.security.KeyStore;

public class HttpsGetWithCustomSSLExample {
    public static void main(String[] args) {
        try {
            // URL of the API endpoint you want to query
            String apiUrl = "https://192.168.14.193:8050/sample-v1";

            // Load trust store
            KeyStore trustStore = KeyStore.getInstance("JKS");
            try (FileInputStream trustStoreInputStream = new FileInputStream("store/server.truststore")) {
                trustStore.load(trustStoreInputStream, "WalletPasswd123".toCharArray());
            }

            // Load keystore
            KeyStore keyStore = KeyStore.getInstance("JKS");
            try (FileInputStream keyStoreInputStream = new FileInputStream("store/server.keystore")) {
                keyStore.load(keyStoreInputStream, "WalletPasswd123".toCharArray());
            }

            // Create SSL context with custom trust store and keystore
            SSLContext sslContext = SSLContextBuilder.create()
                    .loadTrustMaterial(trustStore, null)
                    .loadKeyMaterial(keyStore, "WalletPasswd123".toCharArray())
                    .setProtocol("TLSv1.3")
                    .build();

            // Create a CloseableHttpClient with SSL context
            CloseableHttpClient httpClient = HttpClients.custom().setSSLContext(sslContext).build();

            // Create an HttpGet request
            HttpGet httpGet = new HttpGet(apiUrl);

            // Execute the request
            try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
                // Get the response code
                int responseCode = response.getStatusLine().getStatusCode();
                System.out.println("Response Code: " + responseCode);

                // Read the response content
                String responseBody = EntityUtils.toString(response.getEntity());
                System.out.println("Response Data: " + responseBody);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

This is the error massage I'm getting.

enter image description here

0

There are 0 best solutions below