Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apache httpclient 4.3 not timing out

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.

like image 442
Greg Avatar asked Feb 06 '14 18:02

Greg


People also ask

How do I set HttpClient timeout?

The default value is 100,000 milliseconds (100 seconds). To set an infinite timeout, set the property value to InfiniteTimeSpan. A Domain Name System (DNS) query may take up to 15 seconds to return or time out.

What is connection timeout in HttpClient?

timeout) – the time waiting for data – after establishing the connection; maximum time of inactivity between two data packets. the Connection Manager Timeout (http. connection-manager. timeout) – the time to wait for a connection from the connection manager/pool.

What is the default timeout for HTTP request?

The default value is 60 seconds. If the value of this stanza entry is set to 0 (or not set), connection timeouts between data fragments are governed instead by the client-connect-timeout stanza entry. The exception to this rule occurs for responses returned over HTTP (TCP).


1 Answers

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.

like image 85
Jonah S Avatar answered Sep 19 '22 03:09

Jonah S