apache httpclient 4.3 not timing out
Asked Answered
X

1

7

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);

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.

Xavler answered 6/2, 2014 at 18:54 Comment(0)
H
5

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.

Hydrobomb answered 10/3, 2014 at 0:47 Comment(6)
For reference (it appears to be fixed in 4.3.4): issues.apache.org/jira/browse/HTTPCLIENT-1478Xavler
Please note that it seems to actually fixed in 4.3.4/4.3.5 when connecting via SSL/HTTPS.Menard
If you read the HTTPCLIENT-1478 fully, you will see that several people are complaining that it is not actually fixed. I am using 4.4.1 and I can confirm that this issue still occurs.Predator
@GaspardPetit You are right. I am using HttpClient 4.3.5 over HTTPS. I have set the connection timeout to 1 minute and the connection not getting closed even after 15 minutesDenis
Just tested this with HttpClient 4.5.2 and it is definitely fixed now. Make sure to set the socket config: PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); connManager.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(35000).build());Stickpin
Ya this what worked for me just now. Used HttpClient 4.5.13 and same as Theo suggested to se default Socket-timeout as PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); connManager.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(35000).build());Shred

© 2022 - 2024 — McMap. All rights reserved.