Java "Remote host closed connection during handshake" 使用 Bittrex HTTP API
Java "Remote host closed connection during handshake" using Bittrex HTTP API
我在发出 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 解决了这个问题。查看所选答案及其评论线程以获取更多信息。
首先,开启SSL debug:-Djavax.net.debug=all
将代码的标准输出重定向到文件并检查它是否存在握手错误。
如果您检查 url 与 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 扩展 and/or specify 这个密码。
可以肯定的是,您应该使用 this 小 java 代码打印出当前支持的密码。如果 ECDHE-ECDSA-AES128-GCM-SHA256 未列出,则这是您的问题。
我在发出 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 解决了这个问题。查看所选答案及其评论线程以获取更多信息。
首先,开启SSL debug:-Djavax.net.debug=all
将代码的标准输出重定向到文件并检查它是否存在握手错误。
如果您检查 url 与 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 扩展 and/or specify 这个密码。
可以肯定的是,您应该使用 this 小 java 代码打印出当前支持的密码。如果 ECDHE-ECDSA-AES128-GCM-SHA256 未列出,则这是您的问题。