Getting SSLHandshakeException in java

1.8k views Asked by At

I ma getting SSL Hand Shake error in eclipse while calling https restful web service from simple java stub but, can access this URL from browser after importing Client Digital Certificate to browser which was shared by service provider. Hiding End point URL for security purpose. Please help me, i am new to consuming https web services from Java Clients.

JAVA stub as below:

 try {
          URL url = new URL("https://*********************");
             conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("enctype","multipart/form-data");
            conn.setRequestProperty("Accept", "text/xml");
            System.out.println("Printing Connetion Object "+conn);
        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(
            (conn.getInputStream()))); 
        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }
        conn.disconnect();
      } catch (MalformedURLException e) {
          System.out.println("MalformedURLException-->"+conn);
        e.printStackTrace();
      } catch (IOException e) {
          System.out.println("IOException-->"+conn);
        e.printStackTrace();

      }

Getting Error as below:

javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
    at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)
    at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:136)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:1720)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:954)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1138)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1165)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1149)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:434)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:166)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:379)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:318)
1

There are 1 answers

1
Vishal On BEST ANSWER

If importing to keystore not resolving your issue.. as i was not able to resolve it. Then add following line of code. It worked for me.

System.setProperty( "javax.net.ssl.keyStore", "D:\\G2B.p12" );  // The path to the .p12 file
          System.setProperty( "javax.net.ssl.keyStorePassword", "****" );  // The password of the p12 file
          System.setProperty( "javax.net.ssl.keyStoreType", "pkcs12" );  // Default is JKS, we're using PKCS12