【问题标题】:Curl Request to https endoint working but java client request fails with Error 'javax.net.ssl.SSLHandshakeException'对 https endoint 的卷曲请求正常工作,但 java 客户端请求失败并出现错误“javax.net.ssl.SSLHandshakeException”
【发布时间】: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

【问题讨论】:

标签: java ssl curl certificate postman


【解决方案1】:

您必须将远程服务器证书添加到java's Truststore。 有几种方法可以做到这一点。 Here 就是一个例子。

【讨论】:

    猜你喜欢
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    相关资源
    最近更新 更多