这次给大家带来的是Restemplate请求https接口。众所周知,https接口通常需要证书才能访问,但我们可以采取一些特殊的方法跳过证书验证。接下来,我哥哥会告诉你具体的代码
配置Restemplate的bean说明:Restemplate是spring-web自带,我们不需要引入任何额外的包
@Configuration
public class RestTemplateConfig {
/**
* Restemplatebean,支持忽略自签证书
* @return Resttemplate对象支持发起https请求
* @throws KeyStoreException 证书异常
* @throws NoSuchAlgorithmException 异常不能使用加密算法
* @throws KeyManagementException 密钥管理异常
*/
@Bean("httpsTemplate")
public RestTemplate createHttpsRestTemplate()
throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
TrustStrategy acceptingTrustStrategy = (x509certificates, authType) -> true;
SSLContext sslContext = SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy)
.build();
SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(
sslContext, new NoopHostnameVerifier());
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(sslFactory)
.build();
HttpComponentsClientHttpRequestFactory factory =
new HttpComponentsClientHttpRequestFactory();
factory.setConnectTimeout(3600000);
factory.setReadTimeout(3600000);
factory.setHttpClient(httpClient);
return new RestTemplate(factory);
}
}
使用时,只需根据以下模板传输参数即可
文章为作者独立观点,不代表股票交易接口观点