java - 带有 TLS 的 REST 客户端返回 SSLHandshakeException:收到致命警报:handshake_failure
问题描述
我想与 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)
我在这里缺少什么,我找不到
解决方案
尝试从浏览器下载 REST API 的证书并将该证书添加到您的 truststore.jks 文件中。
按照此链接创建密钥库和信任库:创建密钥库和信任库
推荐阅读
- unix - 计算 UNIX 文件中字符串的出现次数
- c++ - 为什么比较 time_t 值时我的 While-Loop 没有停止?
- c# - 如何检查总用户输入是否已包含在括号中
- excel - EXCEL 根据条件返回值范围
- amazon-web-services - 如何避免 AWS 费用?
- c# - 如何通过 id 作为 db 获取会话数据?
- gulp - Gulp 4:“您是否忘记在监视任务中发出异步完成信号”
- xslt - 对 XSLT 排序后对子项进行分组
- python - 从 get_or_create 结果分配时 ForeignKey 不保存
- unit-testing - 如何检查是否在 Rust 中调用了函数?