用于restful webservice的http客户端获取SSL握手异常

时间:2016-10-31 14:06:11

标签: java web-services rest httpclient restful-authentication

我从我的Java代码中获得SSL握手异常

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

我的Java文件

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();
    }
}
}

使用Postman

检索order_id信息时,相同的第三方网络服务(Tango)有效

这意味着终端服务在我提供的授权下运行良好。

enter image description here

Platform_Name - CareHeroTest

Platform_key - 8heOCfIEJHgarCraHuvqQ6vcajgu8A1r7YU4jUFLjxNtwbuREfBuO54E

授权 - Basic VGFuZ29UZXN0OjV4SXRyM2RNRGxFV0FhOVM0czd2WWg3a1EwMWQ1U0ZlUFBVb1paaUsvdk1mYm8zQTVCdkpMQW1ENHRJPQ==

我做错了什么?由于Tango没有给出其他握手或证书。

1 个答案:

答案 0 :(得分:0)

我使用了unirest API,但它确实有用。

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();
}
}