title: Make HttpClient consume "garbage" before next request
body: <p>I am using Apache HttpClient 4.5.1 to send some requests to our server. To improve performance I'd like to reuse the same HttpClient instance for all those requests. The requests (mostly) happen in sequential order, so there is no multi-threading involved.</p>
  
  <p>Unfortunately some of the servers endpoints are faulty. They are returning a HTTP 204 (No content), but some data in the response body anyway. Even though this has been fixed in the latest release, we will have to deal with the older versions for quite a while. This server behaviour causing the HTTP Client to "hang" (/timeout) on the next request (see <a href="https://stackoverflow.com/questions/32883783/every-second-request-using-apache-httpclient-fails/">Every second request using Apache HTTPClient fails</a> for details).</p>
  
  <p>Is there a way to workaround this on client side? I tried this</p>
  
  <pre><code>HttpEntity entity = response.getEntity();
  if (null != entity) {
      EntityUtils.consumeQuietly(entity);
  }
  </code></pre>
  
  <p>but entity seems to be null, so I cannot consume the response data stuck in connection socket. Is there another way to clean the socket without throwing away the HttpClient instance (and its HTTP connection)?</p>
  
  <p>The client log looks like this:</p>
  
  <pre><code>DefaultManagedHttpClientConnection - http-outgoing-0: set socket timeout to 90000
  MainClientExec - Executing request DELETE /prod/update-rest/private/deleteGroup/AS123 HTTP/1.1
  MainClientExec - Target auth state: UNCHALLENGED
  MainClientExec - Proxy auth state: UNCHALLENGED
  headers - http-outgoing-0 &gt;&gt; DELETE /prod/update-rest/private/deleteGroup/AS123 HTTP/1.1
  headers - http-outgoing-0 &gt;&gt; Host: xyz
  headers - http-outgoing-0 &gt;&gt; Connection: Keep-Alive
  headers - http-outgoing-0 &gt;&gt; User-Agent: Apache-HttpClient/4.5.1 (Java/1.7.0_79)
  headers - http-outgoing-0 &gt;&gt; Accept-Encoding: gzip,deflate
  wire - http-outgoing-0 &gt;&gt; "DELETE /prod/update-rest/private/deleteGroup/AS123 HTTP/1.1[\r][\n]"
  wire - http-outgoing-0 &gt;&gt; "Host: xyz[\r][\n]"
  wire - http-outgoing-0 &gt;&gt; "Connection: Keep-Alive[\r][\n]"
  wire - http-outgoing-0 &gt;&gt; "User-Agent: Apache-HttpClient/4.5.1 (Java/1.7.0_79)[\r][\n]"
  wire - http-outgoing-0 &gt;&gt; "Accept-Encoding: gzip,deflate[\r][\n]"
  wire - http-outgoing-0 &gt;&gt; "[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "HTTP/1.1 401 Unauthorized[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Date: Mon, 05 Oct 2015 13:30:35 GMT[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Server: Apache/2.2.12 (Linux/SUSE)[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Pragma: No-cache[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Cache-Control: no-cache[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Expires: Thu, 01 Jan 1970 01:00:00 CET[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "WWW-Authenticate: Basic realm="ApplicationRealm"[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Content-Length: 958[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Keep-Alive: timeout=15, max=100[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Connection: Keep-Alive[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Content-Type: text/html;charset=utf-8[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "[\r][\n]"
  headers - http-outgoing-0 &lt;&lt; HTTP/1.1 401 Unauthorized
  headers - http-outgoing-0 &lt;&lt; Date: Mon, 05 Oct 2015 13:30:35 GMT
  headers - http-outgoing-0 &lt;&lt; Server: Apache/2.2.12 (Linux/SUSE)
  headers - http-outgoing-0 &lt;&lt; Pragma: No-cache
  headers - http-outgoing-0 &lt;&lt; Cache-Control: no-cache
  headers - http-outgoing-0 &lt;&lt; Expires: Thu, 01 Jan 1970 01:00:00 CET
  headers - http-outgoing-0 &lt;&lt; WWW-Authenticate: Basic realm="ApplicationRealm"
  headers - http-outgoing-0 &lt;&lt; Content-Length: 958
  headers - http-outgoing-0 &lt;&lt; Keep-Alive: timeout=15, max=100
  headers - http-outgoing-0 &lt;&lt; Connection: Keep-Alive
  headers - http-outgoing-0 &lt;&lt; Content-Type: text/html;charset=utf-8
  MainClientExec - Connection can be kept alive for 15000 MILLISECONDS
  HttpAuthenticator - Authentication required
  HttpAuthenticator - xyz:443 requested authentication
  TargetAuthenticationStrategy - Authentication schemes in the order of preference: [Negotiate, Kerberos, NTLM, Digest, Basic]
  TargetAuthenticationStrategy - Challenge for Negotiate authentication scheme not available
  TargetAuthenticationStrategy - Challenge for Kerberos authentication scheme not available
  TargetAuthenticationStrategy - Challenge for NTLM authentication scheme not available
  TargetAuthenticationStrategy - Challenge for Digest authentication scheme not available
  HttpAuthenticator - Selected authentication options: [BASIC [complete=true]]
  wire - http-outgoing-0 &lt;&lt; "&lt;html&gt;&lt;head&gt;&lt;title&gt;JBoss Web/7.0.13.Final - Error report&lt;/title&gt;&lt;style&gt;&lt;!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--&gt;&lt;/style&gt; &lt;/head&gt;&lt;body&gt;&lt;h1&gt;HTTP Status 401 - &lt;/h1&gt;&lt;HR size="1" noshade="noshade"&gt;&lt;p&gt;&lt;b&gt;type&lt;/b&gt; Status report&lt;/p&gt;&lt;p&gt;&lt;b&gt;message&lt;/b&gt; &lt;u&gt;&lt;/u&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;description&lt;/b&gt; &lt;u&gt;This request requires HTTP authentication ().&lt;/u&gt;&lt;/p&gt;&lt;HR size="1" noshade="noshade"&gt;&lt;h3&gt;JBoss Web/7.0.13.Final&lt;/h3&gt;&lt;/body&gt;&lt;/html&gt;"
  DefaultManagedHttpClientConnection - http-outgoing-0: set socket timeout to 90000
  MainClientExec - Executing request DELETE /prod/update-rest/private/deleteGroup/AS123 HTTP/1.1
  MainClientExec - Target auth state: CHALLENGED
  HttpAuthenticator - Generating response to an authentication challenge using basic scheme
  MainClientExec - Proxy auth state: UNCHALLENGED
  headers - http-outgoing-0 &gt;&gt; DELETE /prod/update-rest/private/deleteGroup/AS123 HTTP/1.1
  headers - http-outgoing-0 &gt;&gt; Host: xyz
  headers - http-outgoing-0 &gt;&gt; Connection: Keep-Alive
  headers - http-outgoing-0 &gt;&gt; User-Agent: Apache-HttpClient/4.5.1 (Java/1.7.0_79)
  headers - http-outgoing-0 &gt;&gt; Accept-Encoding: gzip,deflate
  headers - http-outgoing-0 &gt;&gt; Authorization: Basic dGN1YWRtaW46c3RhcnQ=
  wire - http-outgoing-0 &gt;&gt; "DELETE /prod/update-rest/private/deleteGroup/AS123 HTTP/1.1[\r][\n]"
  wire - http-outgoing-0 &gt;&gt; "Host: xyz[\r][\n]"
  wire - http-outgoing-0 &gt;&gt; "Connection: Keep-Alive[\r][\n]"
  wire - http-outgoing-0 &gt;&gt; "User-Agent: Apache-HttpClient/4.5.1 (Java/1.7.0_79)[\r][\n]"
  wire - http-outgoing-0 &gt;&gt; "Accept-Encoding: gzip,deflate[\r][\n]"
  wire - http-outgoing-0 &gt;&gt; "Authorization: Basic dGN1YWRtaW46c3RhcnQ=[\r][\n]"
  wire - http-outgoing-0 &gt;&gt; "[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "HTTP/1.1 204 No Content[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Date: Mon, 05 Oct 2015 13:30:35 GMT[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Server: Apache/2.2.12 (Linux/SUSE)[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Pragma: No-cache[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Cache-Control: no-cache[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Expires: Thu, 01 Jan 1970 01:00:00 CET[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Content-Length: 22[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Keep-Alive: timeout=15, max=99[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Connection: Keep-Alive[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Content-Type: */*[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "[\r][\n]"
  headers - http-outgoing-0 &lt;&lt; HTTP/1.1 204 No Content
  headers - http-outgoing-0 &lt;&lt; Date: Mon, 05 Oct 2015 13:30:35 GMT
  headers - http-outgoing-0 &lt;&lt; Server: Apache/2.2.12 (Linux/SUSE)
  headers - http-outgoing-0 &lt;&lt; Pragma: No-cache
  headers - http-outgoing-0 &lt;&lt; Cache-Control: no-cache
  headers - http-outgoing-0 &lt;&lt; Expires: Thu, 01 Jan 1970 01:00:00 CET
  headers - http-outgoing-0 &lt;&lt; Content-Length: 22
  headers - http-outgoing-0 &lt;&lt; Keep-Alive: timeout=15, max=99
  headers - http-outgoing-0 &lt;&lt; Connection: Keep-Alive
  headers - http-outgoing-0 &lt;&lt; Content-Type: */*
  MainClientExec - Connection can be kept alive for 15000 MILLISECONDS
  HttpAuthenticator - Authentication succeeded
  TargetAuthenticationStrategy - Caching 'basic' auth scheme for https://xyz:443
  PoolingHttpClientConnectionManager - Connection [id: 0][route: {s}-&gt;https://xyz:443] can be kept alive for 15.0 seconds
  PoolingHttpClientConnectionManager - Connection released: [id: 0][route: {s}-&gt;https://xyz:443][total kept alive: 1; route allocated: 1 of 20; total allocated: 1 of 20]
  RequestAddCookies - CookieSpec selected: default
  RequestAuthCache - Auth cache not set in the context
  PoolingHttpClientConnectionManager - Connection request: [route: {s}-&gt;https://xyz:443][total kept alive: 1; route allocated: 1 of 20; total allocated: 1 of 20]
  PoolingHttpClientConnectionManager - Connection leased: [id: 0][route: {s}-&gt;https://xyz:443][total kept alive: 0; route allocated: 1 of 20; total allocated: 1 of 20]
  DefaultManagedHttpClientConnection - http-outgoing-0: set socket timeout to 90000
  MainClientExec - Executing request DELETE /prod/update-rest/private/deleteGroup/AS678 HTTP/1.1
  MainClientExec - Target auth state: UNCHALLENGED
  MainClientExec - Proxy auth state: UNCHALLENGED
  headers - http-outgoing-0 &gt;&gt; DELETE /prod/update-rest/private/deleteGroup/AS678 HTTP/1.1
  headers - http-outgoing-0 &gt;&gt; Host: xyz
  headers - http-outgoing-0 &gt;&gt; Connection: Keep-Alive
  headers - http-outgoing-0 &gt;&gt; User-Agent: Apache-HttpClient/4.5.1 (Java/1.7.0_79)
  headers - http-outgoing-0 &gt;&gt; Accept-Encoding: gzip,deflate
  wire - http-outgoing-0 &gt;&gt; "DELETE /prod/update-rest/private/deleteGroup/AS678 HTTP/1.1[\r][\n]"
  wire - http-outgoing-0 &gt;&gt; "Host: xyz[\r][\n]"
  wire - http-outgoing-0 &gt;&gt; "Connection: Keep-Alive[\r][\n]"
  wire - http-outgoing-0 &gt;&gt; "User-Agent: Apache-HttpClient/4.5.1 (Java/1.7.0_79)[\r][\n]"
  wire - http-outgoing-0 &gt;&gt; "Accept-Encoding: gzip,deflate[\r][\n]"
  wire - http-outgoing-0 &gt;&gt; "[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Group AS123 not found!"
  wire - http-outgoing-0 &lt;&lt; "HTTP/1.1 401 Unauthorized[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Date: Mon, 05 Oct 2015 13:30:36 GMT[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Server: Apache/2.2.12 (Linux/SUSE)[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Pragma: No-cache[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Cache-Control: no-cache[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Expires: Thu, 01 Jan 1970 01:00:00 CET[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "WWW-Authenticate: Basic realm="ApplicationRealm"[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Content-Length: 958[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Keep-Alive: timeout=15, max=98[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Connection: Keep-Alive[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "Content-Type: text/html;charset=utf-8[\r][\n]"
  wire - http-outgoing-0 &lt;&lt; "[\r][\n]"
  DefaultHttpResponseParser - Garbage in response: Group AS123 not found!HTTP/1.1 401 Unauthorized
  DefaultHttpResponseParser - Garbage in response: Date: Mon, 05 Oct 2015 13:30:36 GMT
  DefaultHttpResponseParser - Garbage in response: Server: Apache/2.2.12 (Linux/SUSE)
  DefaultHttpResponseParser - Garbage in response: Pragma: No-cache
  </code></pre>