【问题标题】:http client for restful webservice gets SSL handshake exception用于 Restful Web 服务的 http 客户端获取 SSL 握手异常
【发布时间】:2017-03-13 15:12:36
【问题描述】:

我的 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)工作

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

平台名称 - CareHeroTest

Platform_key - 8heOCfIEJHgarCraHuvqQ6vcajgu8A1r7YU4jUFLjxNtwbuREfBuO54E

授权 - Basic VGFuZ29UZXN0OjV4SXRyM2RNRGxFV0FhOVM0czd2WWg3a1EwMWQ1U0ZlUFBVb1paaUsvdk1mYm8zQTVCdkpMQW1ENHRJPQ==

我做错了什么?因为 Tango 没有提供其他握手或证书。

【问题讨论】:

  • 您是否将 SSL 证书导入 java keytool?
  • 我想不需要 SSL。这应该是基本的 HTTP 事务,具有基本的访问身份验证。我想我需要执行Base64 encoding
  • 这是一个 SSL 异常,而不是 403 Not Authorized HTTP 状态。这意味着您的请求永远不会传输到服务器。在 Postman 示例中,您显然使用了 https://,其中涉及 SSL 握手。后者失败,因为您可能没有导入证书。

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


【解决方案1】:

我使用了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();
}
}

【讨论】:

    猜你喜欢
    • 2014-02-09
    • 1970-01-01
    • 2016-09-23
    • 2013-10-27
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 2017-10-09
    • 1970-01-01
    相关资源
    最近更新 更多