title: Apache httpclient 4.x 302 redirects with keepalive off?
body: <p>I've been trying to make an HTTP GET request to a server which returns a 302 with a redirect path using HttpClient 4.4.1 (GA). I'm having some issues making it work seamlessly when not using keepalive.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>    HttpGet httpget = new HttpGet("http://10.104.107.22:1354/video/Manifest");
      Header header = new BasicHeader(HttpHeaders.CONNECTION,
              "close");
      ArrayList&lt;Header&gt; headers = new ArrayList&lt;&gt;();
      headers.add(header);
      CloseableHttpClient client = HttpClients.custom().setDefaultHeaders(headers).build();
      HttpEntity entity = null;
      CloseableHttpResponse response = client.execute(httpget);
  
      try{    
          entity = response.getEntity();  
              if (entity != null) {
                  long len = entity.getContentLength();
                  if (len != -1 &amp;&amp; len &lt; 2048) {
                      responseBody = EntityUtils.toString(entity);
                  } 
                  else {
                      InputStream instream = entity.getContent();
                      try {
                          StringWriter writer = new StringWriter();
                          IOUtils.copy(instream, writer, "UTF-8");
                          responseBody = writer.toString();
                      } finally {
                          instream.close();
                      }
                  }
              }
      }
      finally{
          EntityUtils.consume(entity);
          response.close();
          client.close();
      }
  </code></pre>
  
  <p>Pretty straightforward. </p>
  
  <p>When getting the GET request, the server responds with</p>
  
  <pre><code>    HTTP/1.1 302 Moved Temporarily
      Content-Length: 0
      Location: http://10.104.107.22:1354/video/Manifest?sessionID=250849653588053716653279720260905006780
      Date: Mon, 06 Apr 2015 19:18:42 GMT
  </code></pre>
  
  <p>Cool, now we know where to go. The problem is, the httpclient then complains with the error:</p>
  
  <pre><code>    Apr 08, 2015 2:39:29 PM org.apache.http.impl.execchain.RetryExec execute
      INFO: I/O exception (org.apache.http.NoHttpResponseException) caught when processing request to /127.0.0.1-&gt;{}-&gt;http://10.104.107.22:1354:         The target server failed to respond
      Apr 08, 2015 2:39:29 PM org.apache.http.impl.execchain.RetryExec execute
      INFO: Retrying request to /127.0.0.1-&gt;{}-&gt;http://10.104.107.22:1354
  </code></pre>
  
  <p>True to its word, it then retries to recover from the IOException and sends another request with the redirect URL, in the same connection!</p>
  
  <pre><code>    GET /video/Manifest?sessionID=250849653588053716653279720260905006780 HTTP/1.1
      Connection: close
      Host: 10.104.107.22:1354
      User-Agent: Apache-HttpClient/4.4.1 (Java 1.5 minimum; Java/1.7.0_55)
      Accept-Encoding: gzip,deflate
  </code></pre>
  
  <p>Now, since we I specified "Connection: close" as the header since the beginning, the server catches on by the this point and closes the socket on its side. My client then gets a RST signal. The request is resent again for the redirected Url, and things work this time. </p>
  
  <p>The problem I'm trying to solve is how to stop the client from sending the redirect request in the same connection. I managed to "solve" this by disabling redirects after the first request with <code>disableRedirectHandling()</code> on the CloseableHttpClient object and then processing the 302 response myself in a separate connection. If I use keepAlive to deal with this problem, things also work fine, although then I have to timeout the client socket with a <code>ConnectionKeepAliveStrategy</code> strategy after I'm done.  </p>
  
  <p>Is there a way the HttpClient 4.4.1 can handle the processing of 302 redirects in separate requests automatically? </p>
  
  <p>EDIT: Posting the complete communication for the GET request as asked.</p>
  
  <pre><code>    GET /video/Manifest HTTP/1.1
      Connection: close
      Host: 10.104.107.22:1354
      User-Agent: Apache-HttpClient/4.4.1 (Java 1.5 minimum; Java/1.7.0_55)
      Accept-Encoding: gzip,deflate
  
      HTTP/1.1 302 Moved Temporarily
      Content-Length: 0
      Location: http://10.104.107.22:1354/video/Manifest?sessionID=250849653588053716653279720260905006780
      Date: Wed, 08 Apr 2015 14:36:42 GMT
  
      GET /video/Manifest?sessionID=250849653588053716653279720260905006780 HTTP/1.1
      Connection: close
      Host: 10.104.107.22:1354
      User-Agent: Apache-HttpClient/4.4.1 (Java 1.5 minimum; Java/1.7.0_55)
      Accept-Encoding: gzip,deflate
  </code></pre>
  
  <p>I then get the RST signal from the server.  </p>
  
  <p>Header Wire + context logging.</p>
  
  <pre><code>    2015/04/08 15:57:25:657 EDT [DEBUG] RequestAddCookies - CookieSpec selected: default
      2015/04/08 15:57:25:667 EDT [DEBUG] RequestAuthCache - Auth cache not set in the context
      2015/04/08 15:57:25:668 EDT [DEBUG] PoolingHttpClientConnectionManager - Connection request: [route: /127.0.0.1-&gt;{}-&gt;http://10.104.107.22:1354][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
      2015/04/08 15:57:25:679 EDT [DEBUG] PoolingHttpClientConnectionManager - Connection leased: [id: 0][route: /127.0.0.1-&gt;{}-&gt;http://10.104.107.22:1354][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]
      2015/04/08 15:57:25:681 EDT [DEBUG] MainClientExec - Opening connection /127.0.0.1-&gt;{}-&gt;http://10.104.107.22:1354
      2015/04/08 15:57:25:683 EDT [DEBUG] DefaultHttpClientConnectionOperator - Connecting to /10.104.107.22:1354
      2015/04/08 15:57:25:686 EDT [DEBUG] DefaultHttpClientConnectionOperator - Connection established 127.0.0.1:46323&lt;-&gt;10.104.107.22:1354
      2015/04/08 15:57:25:686 EDT [DEBUG] MainClientExec - Executing request GET /video/Manifest HTTP/1.1
      2015/04/08 15:57:25:686 EDT [DEBUG] MainClientExec - Target auth state: UNCHALLENGED
      2015/04/08 15:57:25:687 EDT [DEBUG] MainClientExec - Proxy auth state: UNCHALLENGED
      2015/04/08 15:57:25:688 EDT [DEBUG] headers - http-outgoing-0 &gt;&gt; GET /video/Manifest HTTP/1.1
      2015/04/08 15:57:25:688 EDT [DEBUG] headers - http-outgoing-0 &gt;&gt; Connection: close
      2015/04/08 15:57:25:688 EDT [DEBUG] headers - http-outgoing-0 &gt;&gt; Host: 10.104.107.22:1354
      2015/04/08 15:57:25:688 EDT [DEBUG] headers - http-outgoing-0 &gt;&gt; User-Agent: Apache-HttpClient/4.4.1 (Java/1.7.0_55)
      2015/04/08 15:57:25:688 EDT [DEBUG] headers - http-outgoing-0 &gt;&gt; Accept-Encoding: gzip,deflate
      2015/04/08 15:57:25:700 EDT [DEBUG] headers - http-outgoing-0 &lt;&lt; HTTP/1.1 302 Moved Temporarily
      2015/04/08 15:57:25:700 EDT [DEBUG] headers - http-outgoing-0 &lt;&lt; Content-Length: 0
      2015/04/08 15:57:25:701 EDT [DEBUG] headers - http-outgoing-0 &lt;&lt; Location: http://10.104.107.22:1354/video/Manifest?sessionID=162729772390321694625639849465722594850
      2015/04/08 15:57:25:701 EDT [DEBUG] headers - http-outgoing-0 &lt;&lt; Date: Wed, 08 Apr 2015 19:57:25 GMT
      2015/04/08 15:57:25:707 EDT [DEBUG] MainClientExec - Connection can be kept alive indefinitely
      2015/04/08 15:57:25:707 EDT [DEBUG] PoolingHttpClientConnectionManager - Connection [id: 0][route: /127.0.0.1-&gt;{}-&gt;http://10.104.107.22:1354] can be kept alive indefinitely
      2015/04/08 15:57:25:707 EDT [DEBUG] PoolingHttpClientConnectionManager - Connection released: [id: 0][route: /127.0.0.1-&gt;{}-&gt;http://10.104.107.22:1354][total kept alive: 1; route allocated: 1 of 2; total allocated: 1 of 20]
      2015/04/08 15:57:25:709 EDT [DEBUG] DefaultRedirectStrategy - Redirect requested to location 'http://10.104.107.22:1354/video/Manifest?sessionID=162729772390321694625639849465722594850'
      2015/04/08 15:57:25:713 EDT [DEBUG] RedirectExec - Redirecting to 'http://10.104.107.22:1354/video/Manifest?sessionID=162729772390321694625639849465722594850' via /127.0.0.1-&gt;{}-&gt;http://10.104.107.22:1354
      2015/04/08 15:57:25:714 EDT [DEBUG] RequestAddCookies - CookieSpec selected: default
      2015/04/08 15:57:25:714 EDT [DEBUG] RequestAuthCache - Auth cache not set in the context
      2015/04/08 15:57:25:714 EDT [DEBUG] PoolingHttpClientConnectionManager - Connection request: [route: /127.0.0.1-&gt;{}-&gt;http://10.104.107.22:1354][total kept alive: 1; route allocated: 1 of 2; total allocated: 1 of 20]
      2015/04/08 15:57:25:715 EDT [DEBUG] PoolingHttpClientConnectionManager - Connection leased: [id: 0][route: /127.0.0.1-&gt;{}-&gt;http://10.104.107.22:1354][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]
      2015/04/08 15:57:25:715 EDT [DEBUG] MainClientExec - Executing request GET /video/Manifest?sessionID=162729772390321694625639849465722594850 HTTP/1.1
      2015/04/08 15:57:25:715 EDT [DEBUG] MainClientExec - Target auth state: UNCHALLENGED
      2015/04/08 15:57:25:716 EDT [DEBUG] MainClientExec - Proxy auth state: UNCHALLENGED
      2015/04/08 15:57:25:716 EDT [DEBUG] headers - http-outgoing-0 &gt;&gt; GET /video/Manifest?sessionID=162729772390321694625639849465722594850 HTTP/1.1
      2015/04/08 15:57:25:716 EDT [DEBUG] headers - http-outgoing-0 &gt;&gt; Connection: close
      2015/04/08 15:57:25:716 EDT [DEBUG] headers - http-outgoing-0 &gt;&gt; Host: 10.104.107.22:1354
      2015/04/08 15:57:25:716 EDT [DEBUG] headers - http-outgoing-0 &gt;&gt; User-Agent: Apache-HttpClient/4.4.1 (Java/1.7.0_55)
      2015/04/08 15:57:25:717 EDT [DEBUG] headers - http-outgoing-0 &gt;&gt; Accept-Encoding: gzip,deflate
      2015/04/08 15:57:25:717 EDT [DEBUG] DefaultManagedHttpClientConnection - http-outgoing-0: Close connection
      2015/04/08 15:57:25:718 EDT [DEBUG] DefaultManagedHttpClientConnection - http-outgoing-0: Shutdown connection
      2015/04/08 15:57:25:718 EDT [DEBUG] MainClientExec - Connection discarded
      2015/04/08 15:57:25:718 EDT [DEBUG] DefaultManagedHttpClientConnection - http-outgoing-0: Close connection
      2015/04/08 15:57:25:718 EDT [DEBUG] PoolingHttpClientConnectionManager - Connection released: [id: 0][route: /127.0.0.1-&gt;{}-&gt;http://10.104.107.22:1354][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
      2015/04/08 15:57:25:718 EDT [INFO] RetryExec - I/O exception (org.apache.http.NoHttpResponseException) caught when processing request to /127.0.0.1-&gt;{}-&gt;http://10.104.107.22:1354: The target server failed to respond
      2015/04/08 15:57:25:718 EDT [DEBUG] RetryExec - The target server failed to respond &lt;org.apache.http.NoHttpResponseException: The target server failed to respond&gt;org.apache.http.NoHttpResponseException: The target server failed to respond
          at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:143)
          at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
          at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:261)
          at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:165)
          at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:167)
          at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:272)
          at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:124)
          at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:271)
          at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
          at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
          at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
          at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
          at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
          at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
          at WorkingNonKeepAliveUseCase.requestWithoutKeepAlive(WorkingNonKeepAliveUseCase.java:109)
          at Runner.main(Runner.java:13)
  
      2015/04/08 15:57:25:720 EDT [INFO] RetryExec - Retrying request to /127.0.0.1-&gt;{}-&gt;http://10.104.107.22:1354
      2015/04/08 15:57:25:720 EDT [DEBUG] RequestAddCookies - CookieSpec selected: default
      2015/04/08 15:57:25:720 EDT [DEBUG] RequestAuthCache - Auth cache not set in the context
      2015/04/08 15:57:25:720 EDT [DEBUG] PoolingHttpClientConnectionManager - Connection request: [route: /127.0.0.1-&gt;{}-&gt;http://10.104.107.22:1354][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
      2015/04/08 15:57:25:721 EDT [DEBUG] PoolingHttpClientConnectionManager - Connection leased: [id: 1][route: /127.0.0.1-&gt;{}-&gt;http://10.104.107.22:1354][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]
      2015/04/08 15:57:25:721 EDT [DEBUG] MainClientExec - Opening connection /127.0.0.1-&gt;{}-&gt;http://10.104.107.22:1354
      2015/04/08 15:57:25:721 EDT [DEBUG] DefaultHttpClientConnectionOperator - Connecting to /10.104.107.22:1354
      2015/04/08 15:57:25:721 EDT [DEBUG] DefaultHttpClientConnectionOperator - Connection established 127.0.0.1:40753&lt;-&gt;10.104.107.22:1354
      2015/04/08 15:57:25:721 EDT [DEBUG] MainClientExec - Executing request GET /video/Manifest?sessionID=162729772390321694625639849465722594850 HTTP/1.1
      2015/04/08 15:57:25:721 EDT [DEBUG] MainClientExec - Target auth state: UNCHALLENGED
      2015/04/08 15:57:25:721 EDT [DEBUG] MainClientExec - Proxy auth state: UNCHALLENGED
      2015/04/08 15:57:25:722 EDT [DEBUG] headers - http-outgoing-1 &gt;&gt; GET /video/Manifest?sessionID=162729772390321694625639849465722594850 HTTP/1.1
      2015/04/08 15:57:25:722 EDT [DEBUG] headers - http-outgoing-1 &gt;&gt; Connection: close
      2015/04/08 15:57:25:722 EDT [DEBUG] headers - http-outgoing-1 &gt;&gt; Host: 10.104.107.22:1354
      2015/04/08 15:57:25:722 EDT [DEBUG] headers - http-outgoing-1 &gt;&gt; User-Agent: Apache-HttpClient/4.4.1 (Java/1.7.0_55)
      2015/04/08 15:57:25:722 EDT [DEBUG] headers - http-outgoing-1 &gt;&gt; Accept-Encoding: gzip,deflate
      2015/04/08 15:57:25:792 EDT [DEBUG] headers - http-outgoing-1 &lt;&lt; HTTP/1.1 200 OK
      2015/04/08 15:57:25:792 EDT [DEBUG] headers - http-outgoing-1 &lt;&lt; Content-Length: 17090
      2015/04/08 15:57:25:792 EDT [DEBUG] headers - http-outgoing-1 &lt;&lt; Pragma: no-cache
      2015/04/08 15:57:25:793 EDT [DEBUG] headers - http-outgoing-1 &lt;&lt; Cache-Control: max-age=0, no-cache, no-store
      2015/04/08 15:57:25:793 EDT [DEBUG] headers - http-outgoing-1 &lt;&lt; Content-Type: application/vnd.ms-sstr+xml
      2015/04/08 15:57:25:793 EDT [DEBUG] headers - http-outgoing-1 &lt;&lt; Date: Wed, 08 Apr 2015 19:57:25 GMT
      2015/04/08 15:57:25:794 EDT [DEBUG] MainClientExec - Connection can be kept alive indefinitely
      2015/04/08 15:57:26:795 EDT [DEBUG] PoolingHttpClientConnectionManager - Connection [id: 1][route: /127.0.0.1-&gt;{}-&gt;http://10.104.107.22:1354] can be kept alive indefinitely
      2015/04/08 15:57:26:796 EDT [DEBUG] PoolingHttpClientConnectionManager - Connection released: [id: 1][route: /127.0.0.1-&gt;{}-&gt;http://10.104.107.22:1354][total kept alive: 1; route allocated: 1 of 2; total allocated: 1 of 20]
      2015/04/08 15:57:26:796 EDT [DEBUG] PoolingHttpClientConnectionManager - Connection manager is shutting down
      2015/04/08 15:57:26:796 EDT [DEBUG] DefaultManagedHttpClientConnection - http-outgoing-1: Close connection
      2015/04/08 15:57:26:797 EDT [DEBUG] DefaultManagedHttpClientConnection - http-outgoing-1: Close connection
      2015/04/08 15:57:26:797 EDT [DEBUG] PoolingHttpClientConnectionManager - Connection manager shut down
  </code></pre>