RestTemplate调用Https接口

2021-02-17 13:17

阅读:646

标签:cti   示例   str   name   tpc   key   strategy   play   turn   

本文简单示例 Java 实例化一个可调用 https 请求的 RestTemplate:

public static RestTemplate getRestTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
            @Override
            public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                return true;
            }
        }).build();

        SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext,
                new String[]{"TLSv1"},
                null,
                NoopHostnameVerifier.INSTANCE);

        CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLSocketFactory(csf)
                .build();

        HttpComponentsClientHttpRequestFactory requestFactory =
                new HttpComponentsClientHttpRequestFactory();

        requestFactory.setHttpClient(httpClient);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        return restTemplate;
    }

 

RestTemplate调用Https接口

标签:cti   示例   str   name   tpc   key   strategy   play   turn   

原文地址:https://www.cnblogs.com/miaoying/p/12956961.html


评论


亲,登录后才可以留言!