带有TLS的REST客户端返回SSLHandshakeException:收到致命警报:handshake_failure

时间:2018-07-26 12:31:11

标签: java rest certificate tls1.2

我想与RESTful API集成,并且我有此实用程序方法来发送REST请求并获取响应

    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);

在第三方API文档中,它说我必须使用TLS证书,并且它们包含许多与此相关的文件

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

现在每次我尝试给这些服务打电话

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)

我在这里想念的东西,我找不到

1 个答案:

答案 0 :(得分:1)

尝试从浏览器下载REST API的证书,然后将该证书添加到您的truststore.jks文件中。

点击此链接以创建密钥库和信任库:Create keystore and truststore