【问题标题】:javax.net.ssl.SSLHandshakeException: "Remote host closed connection during handshake" opening an HTTPS page with HTMLUnitjavax.net.ssl.SSLHandshakeException:“握手期间远程主机关闭连接”使用 HTMLUnit 打开 HTTPS 页面
【发布时间】:2017-01-12 11:46:04
【问题描述】:

我正在尝试在 Java 7、Ubuntu Linux 下使用 HTML Unit 2.23 获取 HTTPS 页面 https://portal.mvp.bafin.de/database/AnteileInfo/。 p>

以下代码在几天前仍然有效,但现在我收到下面报告的异常。

问题应该与使用的安全证书的类型有关,但是在我阅读时使用 vm 选项 "-Dhttps.protocols=TLSv1.1,TLSv1.2" 执行代码没有任何改变here

代码:

import java.io.IOException;
import java.net.MalformedURLException;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class SSLTest {

    public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
        final String URL = "https://portal.mvp.bafin.de/database/AnteileInfo/";
        final WebClient wc = new WebClient();
        // the code should be ok without the following line,
        // but even with it, it doesn't work
        // wc.getOptions().setSSLClientProtocols(new String[]{"TLSv1.1","TLSv1.2"});
        final HtmlPage mainPage = wc.getPage(URL);
    }
}

例外情况如下:握手期间远程主机关闭连接

Exception in thread "main" javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:880)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1262)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1289)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1273)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:394)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:353)
    at com.gargoylesoftware.htmlunit.httpclient.HtmlUnitSSLConnectionSocketFactory.connectSocket(HtmlUnitSSLConnectionSocketFactory.java:189)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:141)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
    at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:183)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1358)
    at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1275)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:382)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:304)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:451)
    at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:436)
    at fetchers.bafin.SSLTest.main(SSLTest.java:16)
Caused by: java.io.EOFException: SSL peer shut down incorrectly
    at sun.security.ssl.InputRecord.read(InputRecord.java:352)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:861)
    ... 23 more

如果取消注释以下行

wc.getOptions().setSSLClientProtocols(new String[]{"TLSv1.1","TLSv1.2"});

我得到下面的异常,它给出了一个稍微不同的消息:fatal alert: handshake_failure

Exception in thread "main" 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:1911)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1027)
[...]

有什么建议吗?

【问题讨论】:

标签: java ssl htmlunit tls1.2


【解决方案1】:

问题已解决,升级到 Java 8,正如haihui 在评论中所说的那样(但我在该评论之前刚刚升级)。

在阅读 Oracle's blog about HTTPS debugging 并复制 $JAVA_HOME/lib/security 中建议的 JCE 文件后,我升级到了 Java 8(没有任何改变)。

在看到this site about SSL Site debugging 也无法使用 java 7 连接后,我决定升级到 java 8。

在调试期间,我将此添加到 JRE 参数中

-Djavax.net.debug=all

看看发生了什么。

【讨论】:

    猜你喜欢
    • 2012-08-12
    • 2018-07-29
    • 2017-06-25
    • 2014-12-23
    • 2016-05-27
    • 1970-01-01
    • 2017-03-03
    • 2018-09-27
    相关资源
    最近更新 更多