RestTemplate支持https请求
程序员文章站
2022-03-04 20:24:34
...
设置RestTemplate支持https请求
不多说,直接上代码
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate()
{
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient());
return new RestTemplate(requestFactory);
}
/**
* Apache HttpClient
*
* @return
* @see [类、类#方法、类#成员]
*/
private HttpClient httpClient()
{
// 支持HTTP、HTTPS
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", SSLConnectionSocketFactory.getSocketFactory())
.build();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
connectionManager.setMaxTotal(200);
connectionManager.setDefaultMaxPerRoute(100);
connectionManager.setValidateAfterInactivity(2000);
RequestConfig requestConfig = RequestConfig.custom()
// 服务器返回数据(response)的时间,超时抛出read timeout
.setSocketTimeout(65000)
// 连接上服务器(握手成功)的时间,超时抛出connect timeout
.setConnectTimeout(5000)
// 从连接池中获取连接的超时时间,超时抛出ConnectionPoolTimeoutException
.setConnectionRequestTimeout(1000)
.build();
return HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).setConnectionManager(connectionManager).build();
}
}
上一篇: 移动设备与大数据颠覆传统市场调研方式
下一篇: MSSQL通用分页存储过程