hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Huang, Thomas (388J)" <Thomas.Hu...@jpl.nasa.gov>
Subject TIME_WAIT in httpclient-4.0.2
Date Fri, 10 Sep 2010 01:39:39 GMT

I am new to HttpClient.  I took the example directly from the HttpClient javadoc by invoking
it 10 times.  The program ran fine, but I see 10 TIME_WAIT.  This suggests the example code
is not closing the socket gracefully.  Is this a bug or am I looking at a bad example?  The
problem with TIME_WAIT is the program will eventually hang if I increase the number of iterations.
 Please advise.



import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.*;

public class TimeWait {

   public static void test (String url) throws Exception {
      HttpClient httpclient = new DefaultHttpClient();

      // Prepare a request object
      HttpGet httpget = new HttpGet(url);

      // Execute the request
      HttpResponse response = httpclient.execute(httpget);

      // Examine the response status

      // Get hold of the response entity
      HttpEntity entity = response.getEntity();

      // If the response does not enclose an entity, there is no need
      // to worry about connection release
      if (entity != null) {
         InputStream instream = entity.getContent();
         try {

            BufferedReader reader = new BufferedReader(
                  new InputStreamReader(instream));
            // do something useful with the response
         } catch (IOException ex) {
            // In case of an IOException the connection will be released
            // back to the connection manager automatically
            throw ex;
         } catch (RuntimeException ex) {
            // In case of an unexpected exception you may want to abort
            // the HTTP request in order to shut down the underlying
            // connection and release it back to the connection manager.
            throw ex;
         } finally {
            // Closing the input stream will trigger connection release

         // When HttpClient instance is no longer needed,
         // shut down the connection manager to ensure
         // immediate deallocation of all system resources

   public static void main (String[] args) throws Exception {
      for (int i=0; i<10; ++i) {

To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org

View raw message