使用Bittrex HTTP API的Java“握手过程中的远程主机关闭连接”

时间:2018-08-29 19:01:23

标签: java unirest

发出HTTP请求时突然出现了这些错误:

Caused by: javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1002)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1413)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1397)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:290)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:259)
at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:125)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:319)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:138)
... 24 more
Caused by: java.io.EOFException: SSL peer shut down incorrectly
at sun.security.ssl.InputRecord.read(InputRecord.java:505)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
... 41 more

它并不总是这样做,它可以正常工作一段时间。然后,我使用了不同的库,甚至使用了不同的IntelliJ项目进行测试,仍然存在相同的问题。我尝试过的事情:

  • 更改我正在使用的端点
  • 将TLS版本设置为TLSv1.2(我在发出浏览器请求时发现)

我可以使用浏览器和Postman加载端点。我的代码(使用Unirest库):

System.setProperty("https.protocols", "TLSv1.2");
System.out.println(Unirest.get("https://bittrex.com/api/v1.1/public/getmarketsummary?market=btc-ltc").asString().getBody());

我对可能导致此问题的原因不知所措,这非常令人沮丧。任何帮助将不胜感激。

编辑:更新至JDK10修复了该问题。有关更多信息,请参见选定的答案及其注释线程。

1 个答案:

答案 0 :(得分:1)

首先,打开SSL debug:-Djavax.net.debug = all

将代码的标准输出重定向到文件,并检查其与握手有关的错误。

如果您检查与nmap或openssl一起使用的网址:

nmap --script ssl-enum-ciphers -p <port> <host>

openssl s_client -connect <host>:<port>

(主机:bittrex.com,端口:443)

您可以看到它使用的是TLSv1.2和ECDHE-ECDSA-AES128-GCM-SHA256密码:

New, TLSv1/SSLv3, Cipher is ECDHE-ECDSA-AES128-GCM-SHA256
Server public key is 256 bit
Secure Renegotiation IS supported
Compression: NONE
Expansion: NONE
No ALPN negotiated
SSL-Session:
    Protocol  : TLSv1.2
    Cipher    : ECDHE-ECDSA-AES128-GCM-SHA256

根据您的JDK版本,您可能需要安装JCE扩展名和/或specify此密码。

可以肯定的是,您应该使用this小Java代码打印出当前支持的密码。如果未列出ECDHE-ECDSA-AES128-GCM-SHA256,这是您的问题。