码迷,mamicode.com
首页 > Web开发 > 详细

RestTemplate调用Https接口

时间:2020-05-25 15:13:33      阅读:654      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!