resttemplate发送https请求
时间: 2023-11-07 18:55:46 浏览: 61
可以通过在RestTemplate上配置SSL证书来发送HTTPS请求。可以使用以下代码:
```java
RestTemplate restTemplate = new RestTemplate();
HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);
SSLContext sslContext = SSLContextBuilder
.create()
.loadTrustMaterial((chain, authType) -> true)
.build();
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory =
new HttpComponentsClientHttpRequestFactory(HttpClients.custom().setSSLContext(sslContext).build());
restTemplate.setRequestFactory(clientHttpRequestFactory);
ResponseEntity<String> response = restTemplate.exchange("https://example.com", HttpMethod.GET, null, String.class);
String responseBody = response.getBody();
```
需要注意的是,在生产环境中,最好使用有效的证书。
阅读全文