简体   繁体   中英

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. 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. 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. only the java code is not able to connect to the endpoint. Now my questions are :
--->is this a certificate issue.
--->if so ,how does curl works without needing the certificate.

Thanks.

NOTE:Following are the things already tried:
-->set the same set of HEADERS present with curl command for the java client connection .
-->also used alternative rest clients (okhttp3) and HttpClient libraries but get the same exception.

Please find the working curl command below:

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:

 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
(i know in my curl request i have set accept as * ,but it works)
Actual Result:

javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

You have to add the remote server certificate to the java's Truststore . There are several ways to do it. Here is one example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM