问题描述
我有麻烦了Apache的HttpClient(4.3)POST请求使用以下代码超时:
I am having trouble getting the Apache HttpClient (4.3) post request to timeout using the following code:
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)。有什么我在执行超时或其他方法使该呼叫在这里做错了已超时,所以我不能无限期地卡住了。
I am getting the connection and port via a proprietary API. When everything goes well this works however sometimes (due to a bad connection I believe) the code hangs forever at client.execute(post). Is there something I am doing wrong here in implementing the timeout or some other method to make that call have a timeout so I'm not stuck indefinitely.
推荐答案
这是HttpClient 4.3中https连接的错误。在尝试SSL握手之前,套接字超时未在套接字上设置。在握手期间,线程可能会挂起等待套接字读取。 HttpClient 4.2没有这个bug。
This is a bug with https connections in HttpClient 4.3. The socket timeout is not set on the socket before the SSL handshake is attempted. The thread may hang waiting on socket read during the handshake. HttpClient 4.2 does not have this bug.
这篇关于apache httpclient 4.3没有超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!