【发布时间】:2019-10-06 09:22:02
【问题描述】:
我正在尝试通过使用 java 客户端发出 GET 请求来连接到 https 端点。端点就像这样'https://domain/path/{token}',但是每次我提出请求时,我都会得到 'javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure' 异常,我搜索了一个很多网站,但没有运气。有趣的是,当我通过 POSTMAN 从不同的主机(Windows)发出相同的请求时,它可以工作,我也复制了从 POSTMAN 生成的 curl 命令并在我的客户端运行的主机上运行,它在那里也可以正常工作。只有 java 代码无法连接到端点。
现在我的问题是:
--->这是证书问题吗。
--->如果是这样,curl如何在不需要证书的情况下工作。
谢谢。
注意:以下是已经尝试过的事情:
-->为 java 客户端连接设置与 curl 命令相同的 HEADERS 集。
--> 还使用了替代的 rest 客户端 (okhttp3) 和 HttpClient 库,但得到了相同的异常。
请在下面找到有效的 curl 命令:
curl -X GET \
https://domain/x/y/XUwDummyTokenRKZ80EnxDCJlM \
-H 'Accept: */*' \
-H 'Cache-Control: no-cache' \
-H 'Connection: keep-alive' \
-H 'Host:hostname' \
-H 'accept-encoding: gzip, deflate' \
-H 'cache-control: no-cache
还有用于连接到 https 端点的 java 客户端代码:
public void printGetResult( String destUrlStr ) {
try {
URL url = new URL(destUrlStr);
HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
conn.setRequestMethod( "GET" );
InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String inputLine;
while ((inputLine = br.readLine()) != null) {
System.out.println(inputLine);
}
br.close();
}catch(Exception e) {
System.out.println("exception is"+e);
}
}
预期结果:json
(我知道在我的 curl 请求中我已将 accept 设置为 * ,但它有效)
实际结果:
javax.net.ssl.SSLHandshakeException:收到致命警报:handshake_failure
【问题讨论】:
-
您可以调试您的 TLS 连接。看这里:access.redhat.com/solutions/973783
标签: java ssl curl certificate postman