httpclient exception "org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection"

httpclient c#
http client exception java
httpclient timeout
httpclient getasync
httpclient c# get json
httpclient sendasync

I am trying to send request to my server, with following code. it failed at 3rd request, always.

import java.nio.charset.Charset;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

public class HttpClientTest {
    private HttpClient client;

    public HttpClientTest() {
        HttpParams params = new BasicHttpParams();
        params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 15000);  
        params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 15000);

        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, "utf-8");
        HttpProtocolParams.setUseExpectContinue(params, true);
        ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager();
        client = new DefaultHttpClient(cm, params);

        while (true) {
            HttpPost mPost = new HttpPost("http://myip/myservice");

            JSONObject json = new JSONObject();
            try {
                json.put("serialNumber", "abcd");
            } catch (JSONException e1) {
            StringEntity s = null;
            try {
                s = new StringEntity(json.toString());
            } catch (UnsupportedEncodingException e) {

            JSONObject response = null;

            System.out.println("HttpClientTest ---> send post");
            HttpResponse mHttpResponse;
            try {
                mHttpResponse = client.execute(mPost);
                System.out.println("HttpClientTest  ---> get response");
                if(mHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                    HttpEntity entity = mHttpResponse.getEntity(); 
                    ContentType contentType = ContentType.getOrDefault(entity);
                    Charset charset = contentType.getCharset();
                    response = new JSONObject(new JSONTokener(new InputStreamReader(entity.getContent(), charset)));

                    System.out.println("HttpClientTest ---> get result:" + response.toString());
                } else {
            } catch (Exception e) {

    public static void main(String[] args) {
        HttpClientTest t = new HttpClientTest();

the exception as following:

org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection
    at org.apache.http.impl.conn.tsccm.ConnPoolByRoute.getEntryBlocking(
    at org.apache.http.impl.conn.tsccm.ConnPoolByRoute$1.getPoolEntry(
    at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager$1.getConnection(
    at org.apache.http.impl.client.DefaultRequestDirector.execute(
    at org.apache.http.impl.client.AbstractHttpClient.execute(
    at org.apache.http.impl.client.AbstractHttpClient.execute(
    at org.apache.http.impl.client.AbstractHttpClient.execute(

I had the same problem and I found the fix. This timeout is because of a connection leak. In my case, I'm using httpDelete method and not consuming the response. Instead, I'm checking the status of the response.

The fix is, the response entity needs to be consumed. In order to ensure the proper release of system resources, one must close the content stream associated with the entity.

So I used EntityUtils.consumeQuietly(response.getEntity()); which ensures that the entity content is fully consumed and the content stream, if exists, is closed.

HttpClient.GetAsync throws an exception, if server response has more than one "​WWW-Authenticate: Basic" header with different realms. There are two main type of exceptions that the user of HttpClient may encounter when executing HTTP methods: transport exceptions. protocol exceptions.

I have fixed it! add mPost.releaseConnection() in finally blocks.

 try {
 } catch (Exception e) {
 } finally {

DO update package org.apache.httpcomponents to 4.2.1

NET tends to feature far more complex (and specific) exception classes content​; HttpClient client = new HttpClient(); HttpResponseMessage  Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception. (Inherited from Exception)

Just put the line where you are getting your response inside try-with-resources and use CloseableHttpResponse instead of HttpResponse like this:

try(final CloseableHttpResponse mHttpResponse = client.execute(mPost);)
 System.out.println("HttpClientTest  ---> get response");
 ....remainder code

The mHttpResponse object will be auto consumed and closed for you

Hope this helps!

Well, surprise, it throws a TaskCanceledException ! So, there's no way to tell from the exception if the request was actually canceled, or if a  There are two major issues with timeout handling in HttpClient: The timeout is defined at the HttpClient level and applies to all requests made with this The exception thrown when the timeout is elapsed doesn’t let you determine the cause of the error.

This can also happen if you're using ApacheHttpClient with DropWizard 0.6.2, which behind the scenes creates an MultiThreadedHttpConnectionManager with a default configuration - and that default configuration only allowed 2 concurrent http connections at a time more info here.

So with this configuration, if your server is getting swamped and making requests to the same host all the time, you'll have max 2 connections allowed running at a time!

Every exception thrown by a HTTP client must implement Http\Client\Exception . HttpClient::sendRequest() can throw one of the following exceptions. Exceptions The request is null. The request message was already sent by the HttpClient instance. The request failed due to an underlying issue such as network connectivity, DNS failure, server certificate validation or timeout.

We were hitting this error when writing and reading parquets via spark. The exact error was: com.amazonaws.SdkClientException: Unable to execute HTTP request: Timeout waiting for connection from pool

We finally figured out that this was related to the upgrade of Hadoop to version 3.1.3 Seems that in this new version the Hadoop, the property name been changed from: fs.s3a.connection.maximum to: spark.hadoop.fs.s3a.connection.maximum Renaming the property in our spark configuration, set us back on track.

This code throws an HttpRequestException exception whenever the HttpStatusCode represents an error. Let's now consider the following HTTP  HttpClient is intended to be instantiated once and re-used throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. This will result in SocketException errors. Below is an example using HttpClient correctly.

Exception represents an exception that is caused during HTTP requests. Public Methods. Hide inherited methods. Method, Description, Defined By. getName  An HttpClient instance is a collection of settings that apply to all requests executed by that instance. In addition, every HttpClient instance uses its own connection pool, isolating its requests from requests executed by other HttpClient instances. The HttpClient also acts as a class to use with filters for more specific HTTP clients.

I tried adding the timeout but, it throws taskcancelexception (not sure if its the right word), but it give me "A task was canceled" exception error. If I  An exception is thrown when an invalid string for a the Uniform Resource Identifier (URI) is passed to the constructor for the Windows.Foundation.Uri object..NET:   The Windows.Foundation.Uri type appears as System.Uri in C# and VB.

recently I came across this blog post from monsters which talks about issues with using HttpClient in following way: As per the blog post, if we dispose the HttpClient after every request it can keep the TCP connections open. This can potentially lead to System.Net.Sockets.SocketException. The correct way as per the post is to create a

  • Had a similar issue for with the Fluent HTTP Client, when ignoring the response. Still have to call execute().discardContent() to avoid connection leaks.
  • So VERY annoying.... The library should discard if you dont consume it... Or at least give you a warning about it...
  • What about response.close()?
  • While this is probably true, it seems like maybe it's intended as a comment on one or more other answers, rather than an answer in its own right, as it doesn't really address the question directly. I'd suggest either editing it to phrase it as an answer to the question, making it a comment on a different answer or creating your own self-answered question, and linking to that in a comment here.