Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top