简体   繁体   English

Curl请求https端点工作但java客户端请求失败,错误为'javax.net.ssl.SSLHandshakeException'

[英]Curl Request to https endoint working but java client request fails with Error 'javax.net.ssl.SSLHandshakeException'

I am trying to connect to an https endpoint by making GET request with java client. 我试图通过使用Java客户端发出GET请求来连接到https端点。 the endpoint is like this ' https://domain/path/ {token}',but every time i make a reqeust i get 'javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure' Exception , i scoured a lot of websites but no luck. 端点就像这个' https:// domain / path / {token}',但每次我做一个reqeust我得到'javax.net.ssl.SSLHandshakeException:收到致命警报:handshake_failure'例外,我搜索了很多网站,但没有运气。 Interestingly when i make the same request via POSTMAN from a different host (windows) it works,also i copied the curl command generated from POSTMAN and ran on the HOST where my client is running it works fine there too. 有趣的是,当我通过POSTMAN从不同的主机(窗口)发出相同的请求时它也可以工作,我也复制了从POSTMAN生成的curl命令并在我的客户端运行的HOST上运行它也可以正常工作。 only the java code is not able to connect to the endpoint. 只有java代码无法连接到端点。 Now my questions are : 现在我的问题是:
--->is this a certificate issue. --->这是证书问题吗?
--->if so ,how does curl works without needing the certificate. --->如果是这样,curl如何在不需要证书的情况下工作。

Thanks. 谢谢。

NOTE:Following are the things already tried: 注意:以下是已经尝试过的事情:
-->set the same set of HEADERS present with curl command for the java client connection . - >使用curl命令为java客户端连接设置相同的HEADERS集。
-->also used alternative rest clients (okhttp3) and HttpClient libraries but get the same exception. - >也使用了备用休息客户端(okhttp3)和HttpClient库,但得到了相同的异常。

Please find the working curl command below: 请在下面找到工作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

also the java client code being used to connect to the https endpoint: 还有用于连接到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); 
     }

    }

Expected result: json 预期结果:json
(i know in my curl request i have set accept as * ,but it works) (我知道在我的curl请求中我已将accept设置为*,但它有效)
Actual Result: 实际结果:

javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure javax.net.ssl.SSLHandshakeException:收到致命警报:handshake_failure

You have to add the remote server certificate to the java's Truststore . 您必须将远程服务器证书添加到java的Truststore There are several ways to do it. 有几种方法可以做到这一点。 Here is one example. 是一个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java -> Flask Https Request: javax.net.ssl.SSLHandshakeException: PKIX path building failed - Java -> Flask Https Request: javax.net.ssl.SSLHandshakeException: PKIX path building failed 客户端SSL SSL:javax.net.ssl.SSLHandshakeException - Client rest SSL java : javax.net.ssl.SSLHandshakeException 获取请求handshake_faliure:javax.net.ssl.SSLHandshakeException - Get request handshake_faliure:javax.net.ssl.SSLHandshakeException javax.net.ssl.SSLHandshakeException:向服务器发送请求时,java.security.cert.CertPathValidatorException - javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException while sending a request to the server Java JNLP错误:javax.net.ssl.SSLHandshakeException - java JNLP error: javax.net.ssl.SSLHandshakeException javax.net.ssl.SSLHandshakeException:null cert chain java error - javax.net.ssl.SSLHandshakeException: null cert chain java error Java:javax.net.ssl.SSLHandshakeException - Java : javax.net.ssl.SSLHandshakeException HTTPS和SSL的安全性:-javax.net.ssl.SSLHandshakeException:证书已过期 - Security with HTTPS and SSL :-javax.net.ssl.SSLHandshakeException: Certificate expired javax.net.ssl.SSLHandshakeException - javax.net.ssl.SSLHandshakeException javax.net.ssl.SSLHandshakeException? - javax.net.ssl.SSLHandshakeException?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM