在 java 中获取 SSLHandshakeException
Getting SSLHandshakeException in java
从简单的 java 存根调用 https restful Web 服务时,我可能会在 eclipse 中遇到 SSL 握手错误,但是,在将客户端数字证书导入到服务提供商共享的浏览器。出于安全目的隐藏端点 URL。
请帮助我,我刚开始使用 Java 客户端的 https Web 服务。
JAVA存根如下:
try {
URL url = new URL("https://*********************");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("enctype","multipart/form-data");
conn.setRequestProperty("Accept", "text/xml");
System.out.println("Printing Connetion Object "+conn);
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) {
System.out.println("MalformedURLException-->"+conn);
e.printStackTrace();
} catch (IOException e) {
System.out.println("IOException-->"+conn);
e.printStackTrace();
}
出现如下错误:
javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:136)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:1720)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:954)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1138)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1165)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1149)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:434)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:166)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:379)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:318)
如果导入到密钥库无法解决您的问题.. 因为我无法解决它。
然后添加以下代码行。它对我有用。
System.setProperty( "javax.net.ssl.keyStore", "D:\G2B.p12" ); // The path to the .p12 file
System.setProperty( "javax.net.ssl.keyStorePassword", "****" ); // The password of the p12 file
System.setProperty( "javax.net.ssl.keyStoreType", "pkcs12" ); // Default is JKS, we're using PKCS12
从简单的 java 存根调用 https restful Web 服务时,我可能会在 eclipse 中遇到 SSL 握手错误,但是,在将客户端数字证书导入到服务提供商共享的浏览器。出于安全目的隐藏端点 URL。 请帮助我,我刚开始使用 Java 客户端的 https Web 服务。
JAVA存根如下:
try {
URL url = new URL("https://*********************");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("enctype","multipart/form-data");
conn.setRequestProperty("Accept", "text/xml");
System.out.println("Printing Connetion Object "+conn);
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) {
System.out.println("MalformedURLException-->"+conn);
e.printStackTrace();
} catch (IOException e) {
System.out.println("IOException-->"+conn);
e.printStackTrace();
}
出现如下错误:
javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:136)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:1720)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:954)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1138)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1165)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1149)
at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:434)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:166)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:379)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:318)
如果导入到密钥库无法解决您的问题.. 因为我无法解决它。 然后添加以下代码行。它对我有用。
System.setProperty( "javax.net.ssl.keyStore", "D:\G2B.p12" ); // The path to the .p12 file
System.setProperty( "javax.net.ssl.keyStorePassword", "****" ); // The password of the p12 file
System.setProperty( "javax.net.ssl.keyStoreType", "pkcs12" ); // Default is JKS, we're using PKCS12