简体   繁体   中英

REST client with TLS returns SSLHandshakeException: Received fatal alert: handshake_failure

I am tying to integrate with RESTful API,and i have this utility method to send REST request and get the response

    BufferedReader br;
        HttpURLConnection con = null;

        try {
            URL obj = new URL(url);
            con = (HttpURLConnection) obj.openConnection();

            con.setReadTimeout(httpReadTimeOut);
            con.setRequestMethod("POST");

            for (String key : requestTypeMap.keySet()) {
                con.setRequestProperty(key, requestTypeMap.get(key));
            }

            String jsonRequest = gson.toJson(request);
            CommonUtilityLogger.debug("json request is :: " + jsonRequest);

            con.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(con.getOutputStream());
            wr.writeBytes(jsonRequest);
            wr.flush();
            wr.close();

            if (con.getResponseCode() != 200) {
                CommonUtilityLogger.error("failed : HTTP error code : " + con.getResponseCode());
            }

            br = new BufferedReader(new InputStreamReader((con.getInputStream())));

            return (T) gson.fromJson(br, responseType);

In the third party API documentation, it says i have to use TLS certificates and they include many files related to that

Test Certificate 1231181189.key 
Test Certificate 1231181189.p7b 
Test Certificate 1231181189.p12 
Test Certificate 1231181189.pem 
TLS Root CA.pem

now every time i try to call these service i got

javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:154)

what i am missing here, i cannot find it out

Try downloading certificate for the REST API from browser and add that certificate in your truststore.jks file.

Follow this link to create keystore and truststore: Create keystore and truststore

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