首页 > 解决方案 > RestTemplate 调用 GET HTTPS Endpoint 问题

问题描述

我尝试从浏览器调用其余端点并且工作正常,但是使用其余模板,我没有得到任何响应。

网址:https ://www1.nseindia.com/marketinfo/sym_map/symbolCount.jsp?symbol=INFY

public class Test {

    public static void main(String[] args) throws Exception {

        try {

            String jksPath = "C:\\ssl_server.jks";
            String pass = "123456";
            SSLContext ssl = SSLContextBuilder.create()
                    .loadTrustMaterial(ResourceUtils.getFile(jksPath), pass.toCharArray()).build();

            SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(ssl, new LHVerifier());
            CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
            HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
            requestFactory.setHttpClient(httpClient);
            requestFactory.setConnectionRequestTimeout(15000);
            requestFactory.setReadTimeout(15000);

            System.out.println("Start: " + new Date());

            HttpHeaders headers = new HttpHeaders();
            headers.set(org.apache.http.HttpHeaders.ACCEPT, "*/*");
            HttpEntity<?> httpEntity = new HttpEntity<>(headers);
            ResponseEntity<Object> response = new RestTemplate(requestFactory).exchange(
                    "https://www1.nseindia.com/marketinfo/sym_map/symbolCount.jsp?symbol=INFY", HttpMethod.GET,
                    httpEntity, Object.class);
        } catch (Exception e) {
            System.out.println("Exception: " + new Date());
            e.printStackTrace();
        }
        System.out.println("done");

    }
}


class LHVerifier implements HostnameVerifier {

    @Override
    public boolean verify(String hostname, SSLSession session) {
        return true;
    }
}

标签: springrestresttemplate

解决方案


推荐阅读