Uploaded image for project: 'HttpComponents HttpClient'
  1. HttpComponents HttpClient
  2. HTTPCLIENT-1887

tcp status remains in ·CLOSE_WAIT· for a very long time

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Invalid
    • Affects Version/s: 4.5.3
    • Fix Version/s: None
    • Component/s: HttpClient (classic)
    • Labels:
      None

      Description

      run the following code, after the request completed, then wait for some seconds, you can use `netstat -an | grep

      {the server ip}

      ` to see the tcp status, the tcp status will transform from `ESTABLISHED` to `CLOSE_WAIT`. and it remains in `CLOSE_WAIT` for a very long time, this only happen when the server want to close the tcp connection, but the client does not send `FIN` to the server.

      package cn.com.duibaboot.ext.autoconfigure.httpclient;
      
      import org.apache.commons.io.IOUtils;
      import org.apache.http.HttpResponse;
      import org.apache.http.client.methods.CloseableHttpResponse;
      import org.apache.http.client.methods.HttpGet;
      import org.apache.http.client.methods.HttpUriRequest;
      import org.apache.http.impl.client.CloseableHttpClient;
      import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
      import org.apache.http.impl.client.HttpClientBuilder;
      import org.apache.http.protocol.HttpContext;
      
      import java.io.IOException;
      
      public class HttpClientTest1 {
      
          public static void main(String[] args) throws InterruptedException {
              final CloseableHttpClient httpClient = HttpClientBuilder
                      .create()
                      .setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy(){
                          @Override
                          public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
                              long time = super.getKeepAliveDuration(response, context);
                              if(time == -1){
                                  time = 30000;
                              }
                              return time;
                          }
                      })
                      .build();
      
              HttpUriRequest req = new HttpGet("http://www.duiba.com.cn");
              try {
                  CloseableHttpResponse r = httpClient.execute(req);
                  IOUtils.toByteArray(r.getEntity().getContent());
                  r.getEntity().getContent().close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
      
              Thread.sleep(Integer.MAX_VALUE);
          }
      }
      
      

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              after_sss wenqi.huang
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: