【发布时间】: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 AuthorizedHTTP 状态。这意味着您的请求永远不会传输到服务器。在 Postman 示例中,您显然使用了https://,其中涉及 SSL 握手。后者失败,因为您可能没有导入证书。
标签: java web-services rest httpclient restful-authentication