我在使用以下代码使 Apache HttpClient (4.3) 发布请求超时时遇到问题:
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(4000).setConnectTimeout(4000)
.setSocketTimeout(4000).build();
CloseableHttpClient client = HttpClients.custom().setSslcontext(sslContext)
.setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
.setDefaultRequestConfig(requestConfig).build();
HttpPost post = new HttpPost("https://localhost:" + connection.getLocalPort() + "/API/webservice.asmx");
StringEntity stringE = new StringEntity(REQUEST);
post.setEntity(stringE);
CloseableHttpResponse response = client.execute(post);
我正在通过专有 API 获取连接和端口。当一切顺利时,这会起作用,但有时(我相信是由于连接不良)代码永远卡在 client.execute(post) 上。在实现超时或其他方法使该调用超时时,我在这里做错了什么,所以我不会无限期地卡住。
最佳答案
这是 HttpClient 4.3 中 https 连接的错误。在尝试 SSL 握手之前,未在套接字上设置套接字超时。在握手期间,线程可能会挂起等待套接字读取。 HttpClient 4.2 没有这个错误。
关于java - apache httpclient 4.3 没有超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21611528/