尝试发送HTTPS获取请求时获取连接超时

时间:2018-09-26 18:23:21

标签: java ssl https proxy get

我尝试通过使用本机Java类,Apache HttpClient和Spring的restTemplate向https://google.com发送HTTPS Get请求,但是所有三个都以相同的问题结束:连接异常:连接超时错误。

调用http://www.google.com的效果很好,只有当我使用https时才会发生超时

我在公司防火墙后面,因此我做了一些阅读,以了解允许请求使用代理的情况。我已经设置了-Dhttp.proxyHost和-Dhttp.proxyPort环境变量,以及-Djavax.net.ssl.trustStore环境变量。我确保Google的根SSL证书在trustStore的合格证书列表中。

理想情况下,我将使用spring的rest模板,所以我有以下代码:

RestTemplate restTemplate = new RestTemplate();
String url = "https://www.google.com";
String response = restTemplate.getForObject(url, String.class);
return new ResponseEntity<String>(response, HttpStatus.OK);

使用调试器运行后,我看到它卡在了第2行,而这本来是应该发出请求的。当我使用Fiddler查看网络流量时,我看到对localhost进行了http调用,这是超时的调用。我从未见过对https://www.google.com进行HTTPS调用。这对我来说很难解决,因为我无法确定这是代理问题还是某些环境设置问题。

这是堆栈跟踪:

Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method) ~[na:1.8.0_161]
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) ~[na:1.8.0_161]
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[na:1.8.0_161]
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[na:1.8.0_161]
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_161]
    at java.net.PlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_161]
    at java.net.SocksSocketImpl.connect(Unknown Source) ~[na:1.8.0_161]
    at java.net.Socket.connect(Unknown Source) ~[na:1.8.0_161]
    at sun.security.ssl.SSLSocketImpl.connect(Unknown Source) ~[na:1.8.0_161]
    at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source) ~[na:1.8.0_161]
    at sun.net.NetworkClient.doConnect(Unknown Source) ~[na:1.8.0_161]
    at sun.net.www.http.HttpClient.openServer(Unknown Source) ~[na:1.8.0_161]
    at sun.net.www.http.HttpClient.openServer(Unknown Source) ~[na:1.8.0_161]
    at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source) ~[na:1.8.0_161]
    at sun.net.www.protocol.https.HttpsClient.New(Unknown Source) ~[na:1.8.0_161]
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source) ~[na:1.8.0_161]
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source) ~[na:1.8.0_161]
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source) ~[na:1.8.0_161]
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source) ~[na:1.8.0_161]
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(Unknown Source) ~[na:1.8.0_161]
    at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:78) ~[spring-web-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53) ~[spring-web-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:660) ~[spring-web-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    ... 91 common frames omitted

0 个答案:

没有答案