Search code examples
javaweb-servicesresthttpclientrestful-authentication

http client for restful webservice gets SSL handshake exception


I get SSL Handshake exception from my Java code

javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.HttpURLConnection.getResponseCode(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
at com.mkyong.rest.client.NetClientGet.main(NetClientGet.java:25)
Caused by: java.io.EOFException: SSL peer shut down incorrectly
at sun.security.ssl.InputRecord.read(Unknown Source)
... 10 more

My Java file

package com.mypackage.rest.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class ClientGet {

public static void main(String[] args) {

    try {

        URL url = new URL(
                "https://sandbox.tangocard.com/raas/v1.1/orders/116-101338692-28");         
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/json");
        conn.setRequestProperty("Platform_Name", "CareHeroTest");
        conn.setRequestProperty("Platform_key","8heOCfIEJHgarCraHuvqQ6vcajgu8A1r7YU4jUFLjxNtwbuREfBuO54E");
        conn.setRequestProperty("Authorization","Basic VGFuZ29UZXN0OjV4SXRyM2RNRGxFV0FhOVM0czd2WWg3a1EwMWQ1U0ZlUFBVb1paaUsvdk1mYm8zQTVCdkpMQW1ENHRJPQ==");

        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) {

        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

The same third party web service (Tango) works when retrieve order_id information using Postman

This means the end service is working fine with the authorization I provide.

enter image description here

Platform_Name - CareHeroTest

Platform_key - 8heOCfIEJHgarCraHuvqQ6vcajgu8A1r7YU4jUFLjxNtwbuREfBuO54E

Authorization - Basic VGFuZ29UZXN0OjV4SXRyM2RNRGxFV0FhOVM0czd2WWg3a1EwMWQ1U0ZlUFBVb1paaUsvdk1mYm8zQTVCdkpMQW1ENHRJPQ==

What am I doing wrong? As there is no other handshake or certificate given by Tango.


Solution

  • I used unirest API and it worked.

    package com.ansari.examples.javahttpapiclient;
    
    import com.mashape.unirest.http.HttpResponse;
    import com.mashape.unirest.http.JsonNode;
    import com.mashape.unirest.http.Unirest;
    
    public class JavaHTTPAPIClient {
    public void getQuestionsUsingUnirest() throws Exception {
        HttpResponse<JsonNode> response = Unirest.get("https://sandbox.tangocard.com/raas/v1.1/orders/116-101338692-28").
                header("accept",  "application/json").
                header("Authorization", "Basic VGFuZ29UZXN0OjV4SXRyM2RNRGxFV0FhOVM0czd2WWg3a1EwMWQ1U0ZlUFBVb1paaUsvdk1mYm8zQTVCdkpMQW1ENHRJPQ==").
                asJson();
        System.out.println(response.getBody().getObject().toString(2));
    }
    
    public static void main(String args[]) throws Exception {
        JavaHTTPAPIClient client = new JavaHTTPAPIClient();
        client.getQuestionsUsingUnirest();
    }
    }