HttpClient访问https

哈希塞特 程序员  2020-07-13 11:39:49
帮忙看下我这段配置,为什么不能访问https的接口

static {
ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
SSLContext sc;
try {
sc = SSLContext.getInstance("TLS");
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager(){
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
} };
sc.init(null, trustAllCerts, null);
} catch (Exception e) {
LOG.error("some thing happen", e);
throw new ServiceException(e);
}
ConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sc, (s, sslSession) -> true);
ConnectionKeepAliveStrategy connectionKeepAliveStrategy = (httpResponse, httpContext) -> {
return 20L * 1000;
};
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create()
.register("http", plainsf).register("ftp", plainsf).register("https", sslsf).build();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(registry);
connManager.setMaxTotal(400);
connManager.setDefaultMaxPerRoute(200);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(HTTP_CONNECT_TIME_OUT)
.setConnectionRequestTimeout(3000).setSocketTimeout(HTTP_TIME_OUT).build();
client = HttpClients.custom().setConnectionManager(connManager)
.setRetryHandler(new DefaultHttpRequestRetryHandler()).setDefaultRequestConfig(requestConfig)
.setKeepAliveStrategy(connectionKeepAliveStrategy).build();
}
...全文
1108 点赞 收藏 7
写回复
7 条回复
s478853630 2020年12月29日
connect timed out,连接超时 有可能是对方服务的问题,或者端口不通
回复 点赞
qq_26306063 2020年12月29日
跟我的问题一模一样,绝了
回复 点赞
tianfang 2020年12月29日
公司网络使用代理,在dhcp中指定了代理 你的代码没有从系统网络参数中获得代理信息,所以在公司内网无法访问外网
回复 点赞
dkwuxiang 2020年07月13日
错误信息是什么?
回复 点赞
哈希塞特 2020年07月13日
百度上都是这么配的,到我这就不行了。最后返回的client对象按道理应该是能访问https接口的
回复 点赞
哈希塞特 2020年07月13日
现在问题是这样,环境是公司内网,本地用其他http工具访问外网https是可以访问的,就是在java代码中不能访问外部https。
然后我把电脑连上我自己的手机热点,其他什么然后代码中也可以访问外部https了。
问题应该是网络原因,但是公司内网中为什么用postman也能访问https呢?
回复 点赞
哈希塞特 2020年07月13日
引用 2 楼 dkwuxiang 的回复:
错误信息是什么?


15:37:14.226 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Opening connection {s}->https://www.baidu.com:443
15:37:14.241 [main] DEBUG org.apache.http.impl.conn.DefaultHttpClientConnectionOperator - Connecting to www.baidu.com/180.101.49.11:443
15:37:14.241 [main] DEBUG org.apache.http.conn.ssl.SSLConnectionSocketFactory - Connecting socket to www.baidu.com/180.101.49.11:443 with timeout 15000
15:37:29.244 [main] DEBUG org.apache.http.impl.conn.DefaultHttpClientConnectionOperator - Connect to www.baidu.com/180.101.49.11:443 timed out. Connection will be retried using another IP address
15:37:29.244 [main] DEBUG org.apache.http.impl.conn.DefaultHttpClientConnectionOperator - Connecting to www.baidu.com/180.101.49.12:443
15:37:29.244 [main] DEBUG org.apache.http.conn.ssl.SSLConnectionSocketFactory - Connecting socket to www.baidu.com/180.101.49.12:443 with timeout 15000
15:37:44.247 [main] DEBUG org.apache.http.impl.conn.DefaultManagedHttpClientConnection - http-outgoing-0: Shutdown connection
15:37:44.248 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Connection discarded
15:37:44.249 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection released: [id: 0][route: {s}->https://www.baidu.com:443][total kept alive: 0; route allocated: 0 of 200; total allocated: 0 of 400]
15:37:44.249 [main] ERROR com.suning.itp.thor.common.utils.RestClient - 发送https://www.baidu.com 次数:0 请求返回信息:{},{}
15:37:44.250 [main] ERROR com.suning.itp.thor.common.utils.RestClient - http请求出现异常,次数:0,信息:org.apache.http.conn.ConnectTimeoutException,Connect to www.baidu.com:443 [www.baidu.com/180.101.49.11, www.baidu.com/180.101.49.12] failed: connect timed out
15:37:46.258 [main] ERROR com.suning.itp.thor.common.utils.RestClient - http请求失败
org.apache.http.conn.ConnectTimeoutException: Connect to www.baidu.com:443 [www.baidu.com/180.101.49.11, www.baidu.com/180.101.49.12] failed: connect timed out
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:151)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:373)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:394)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
at com.suning.itp.thor.common.utils.RestClient.httpExecute(RestClient.java:326)
at com.suning.itp.thor.common.utils.RestClient.httpGet(RestClient.java:112)
at com.suning.itp.thor.common.utils.RestClient.httpGet(RestClient.java:107)
at com.suning.itp.thor.common.utils.RestClient.main(RestClient.java:103)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:339)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
... 13 common frames omitted
Exception in thread "main" org.apache.http.conn.ConnectTimeoutException: Connect to www.baidu.com:443 [www.baidu.com/180.101.49.11, www.baidu.com/180.101.49.12] failed: connect timed out
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:151)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:373)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:394)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
at com.suning.itp.thor.common.utils.RestClient.httpExecute(RestClient.java:326)
at com.suning.itp.thor.common.utils.RestClient.httpGet(RestClient.java:112)
at com.suning.itp.thor.common.utils.RestClient.httpGet(RestClient.java:107)
at com.suning.itp.thor.common.utils.RestClient.main(RestClient.java:103)

错误信息就是超时,本地用其他http工具调试都没问题
回复 点赞
发动态
发帖子
Web 开发
创建于2007-09-28

5.2w+

社区成员

34.1w+

社区内容

Java Web 开发
社区公告
暂无公告