java - HTTPS请求的Spring Boot RestTemplate问题
问题描述
我发送到第三方服务器的 RestTemplate HTTPS 请求有问题。每第二个请求都是成功的。在每个第一个 HTTPS 请求上,我都会收到两种类型的错误:
- 软件导致连接中止:recv failed SSLException
- 服务器未能响应 I/O 异常
这两个错误总是在第一次请求时发生,在第二次请求时一切正常。这里是Http请求工厂的配置:
public RestTemplate getRestTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException
{
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy)
.build();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(csf)
.build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate();
restTemplate.setRequestFactory(requestFactory);
return restTemplate;
}
我进行了很多研究,但我只发现了增加读取超时的提示,这在getRestTemplate(...)
方法的第一次实现中是有问题的。现在不限。
这是技术栈:
- Java 1.8
- Spring Boot 1.5.7.RELEASE
- HttpClient 4.4.1。
- Apache Tomcat 9.0。
先感谢您。
解决方案
推荐阅读
- html - html 选择大小或高度在移动浏览器上不起作用
- c# - 实例访问其他实例的属性值
- laravel - Laravel/Lumen - 使用 Model::with()
- powershell - 使用 Powershell 在单独的行上列出完全路径文件名?
- python - 找不到带有参数“(5,)”的Django Reverse
- javascript - 如何在 React JS 的只读 Material UI TextField 中显示闪烁的光标
- c# - ASP.NET Core 3.0:如何将现有身份验证转发到另一个请求 - 双跳问题?
- python - 如何使用python添加嵌入在电子邮件正文中的图像?
- android - NotificationManager.notify() 在屏幕打开时不启动全屏意图
- xml - UPS 运输请求 - XML 表单格式正确但文档无效 (10002)