Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IBM java POST API Throws an SSL HandShake Exception

I have tried to create post API Rest Call by using IBM Java(1.6) which is Out of Box of Websphere server 7(Application Server). While establishing the connection it' throws

Exception in thread "main" java.net.SocketException: java.lang.ClassNotFoundException: Cannot find the specified class com.ibm.websphere.ssl.protocol.SSLSocketFactory

By following these steps in these link

https://www.link-intersystems.com/blog/2014/07/20/how-to-fix-java-lang-classnotfoundexception-com-ibm-websphere-ssl-protocol-sslsocketfactory-in-eclipse/

After Fixing that issue, while posting the request it throws an

Exception in thread "main" javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

Same Code Works Fine with Oracle Java(1.6)

Below code is which I have tried

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.security.Security;

public class POST {

     public static void main(String[] args){


        try {
            POSTRequest();
        } catch (IOException e) {
            System.out.println("Error" + e);
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }
    public static void POSTRequest() throws IOException {
        Security.setProperty("ssl.SocketFactory.provider", "com.ibm.jsse2.SSLSocketFactoryImpl");
        Security.setProperty("ssl.ServerSocketFactory.provider", "com.ibm.jsse2.SSLServerSocketFactoryImpl");
        System.out.println("Begin Post Request");
        final String POST_PARAMS = "{\n" + "\"userId\": 151,\r\n" +
            "    \"id\": 101,\r\n" +
            "    \"title\": \"VM Title\",\r\n" +
            "    \"body\": \"Test Body\"" + "\n}";
        System.out.println(POST_PARAMS);
        URL obj = new URL("https://jsonplaceholder.typicode.com/posts");
        HttpURLConnection postConnection = (HttpURLConnection) obj.openConnection();
        postConnection.setRequestMethod("POST");
        postConnection.setRequestProperty("userId", "a1bcdefgh");
        postConnection.setRequestProperty("Content-Type", "application/json");
        postConnection.setDoOutput(true);
        OutputStream os = postConnection.getOutputStream();
        os.write(POST_PARAMS.getBytes());
        os.flush();
        os.close();
        int responseCode = postConnection.getResponseCode();
        System.out.println("POST Response Code :  " + responseCode);
        System.out.println("POST Response Message : " + postConnection.getResponseMessage());
        if (responseCode == HttpURLConnection.HTTP_CREATED) { //success
            BufferedReader in = new BufferedReader(new InputStreamReader(
                postConnection.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in .readLine()) != null) {
                response.append(inputLine);
            } in .close();
            // print result
            System.out.println(response.toString());
        } else {
            System.out.println("POST NOT WORKED");
        }
    }




}

Post_API.txt Displaying Post_API.txt.

Please advise me to resolve this issues.

like image 672
Debugger Avatar asked Oct 09 '18 14:10

Debugger


2 Answers

You may try and set the protocol in the request being submitted to the Web Service.

System.setProperty(“https.protocols”, “TLSv1,TLSv1.1,TLSv1.2”);

Also, a thing to note would be the version of the Fix pack and the Java version being used in WebSphere. WebSphere uses its own version of Java that is bundled with the installation.

You may need to upgrade the Java version for the WebSphere installation if it is at a lower update version.

like image 59
Utsav Avatar answered Oct 14 '22 06:10

Utsav


handshake_failure typically has to do with mismatched SSL/TLS protocols or cipher suites.

However, in this case, if you look at Qualys SSL tester for this site, the 4 servers returned show that Java 6 will fail due to unsupported SNI.

So I'd guess either the Oracle Java 6 has some different default SSL settings, or something set to override a default behavior, or maybe the WebSphere Java 6 is an older fix level of Java 6, or the two Javas' sequence of negotiating TLS levels and cipher suites is different.

Also note that WebSphere 7 is now out of support, as is Java 6.

like image 38
dbreaux Avatar answered Oct 14 '22 07:10

dbreaux