gpt4 book ai didi

java - 在 Java 中获取 SSLHandshakeException

转载 作者:太空宇宙 更新时间:2023-11-03 13:07:31 24 4
gpt4 key购买 nike

我在从简单的 java stub 调用 https restful web 服务时在 eclipse 中遇到 SSL Hand Shake 错误,但是,在将客户端数字证书导入服务提供商共享的浏览器后,可以从浏览器访问此 URL。出于安全目的隐藏端点 URL。请帮助我,我是从 Java 客户端使用 https Web 服务的新手。

JAVA stub 如下:

 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)

最佳答案

如果导入到 keystore 无法解决您的问题.. 因为我无法解决它。然后添加以下代码行。它对我有用。

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 - 在 Java 中获取 SSLHandshakeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30931531/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com