Java-收到致命警报:handshake_failure

时间:2018-07-25 14:14:43

标签: java handshake sslhandshakeexception

我正在尝试获取网站的HTML源代码。

我的代码:

public static void main(String[] args) {
    URL url;
    InputStream is;
    try {
        url = new URL("https://www.trackobot.com/");
        is = url.openStream();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

但是,运行此代码时,出现异常:

javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
   at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
   at sun.security.ssl.Alerts.getSSLException(Alerts.java:154)
   at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:2023)
   at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1125)
   at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
   at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
   at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
   at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
   at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
   at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1546)
   at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
   at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
   at java.net.URL.openStream(URL.java:1045)
   at JsoupTutorial.ConnectToUrl.main(ConnectToUrl.java:24)

如果我更改站点,即使使用PayPal之类的https站点,它也能正常工作。但是,我仍然有一些站点无法正常工作并抛出该异常。在该示例中,此站点是其中不起作用的站点之一。

我进行了搜索,了解我需要将此站点的证书导入Java密钥库。 我可以纠正吗?

我的问题是Chrome如何设法进入那些“安全”网站并获取HTML代码?有人必须将证书更新(导入)到Chrome Cer数据库中。.谁做的以及何时发生? (以防万一我在上面是正确的。)

无论如何,我要设法完美地加载那些网站并接收我想要的数据。在哪里可以从基础知识中学习?

谢谢。

1 个答案:

答案 0 :(得分:1)

几乎可以肯定,这里的问题是您正在尝试访问使用SSL / HTTPS的网站,但没有使用支持该网站的API。您可以尝试将HttpsURLConnection与接受所有内容的(坏)信任库一起使用。 永远不要在生产中这样做

SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(new KeyManager[0], new TrustManager[] {new DefaultTrustManager()}, new SecureRandom());
SSLContext.setDefault(ctx);

URL url = new URL("https://www.trackobot.com/");
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setHostnameVerifier(new HostnameVerifier() {
    @Override
    public boolean verify(String arg0, SSLSession arg1) {
        return true;
    }
});
con.setRequestMethod("GET");
System.out.println("Response Code : " + con.getResponseCode());

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
StringBuffer response = new StringBuffer();

while ((line = in.readLine()) != null) {
    response.append(line);
}
in.close();

System.out.println(response.toString());

private static class DefaultTrustManager implements X509TrustManager {
    @Override
    public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}

    @Override
    public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}

    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return null;
    }
}

理想情况下,假设域出示了来自相对标准的提供商的证书,HttpsURLConnection应该可以正常工作。由于它不适用于您的域,因此我们可以将其配置为信任所有内容。生产不好,但暂时可以让您继续。

相关问题