我在使用以下代码时遇到了Apache HttpClient (4.3) post请求超时的问题:
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) 上。我在实现超时方面是否存在问题?或者是否有其他方法可以使调用超时,以便我不会无限期地困在其中。
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); connManager.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(35000).build());
- TheoPoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); connManager.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(35000).build());
。 - Abhishek