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

Connection Timeout doesn't work where there's an UnknownHostException

    XMLWordPrintableJSON

Details

    Description

      When trying to connect to an unresolvable host (using the host name, not the IP), it seems that none of the timeouts has any effect.
      On one of my machines the UnknownHostConnection is only thrown after 20 seconds, even though my timeouts are set to 1 second.
      Please note that the duration of the timeout changes between computers. On my Windows XP machine it takes only 500ms to throw the exception, but on a Linux machine (probably with a more problematic DNS) it takes 20 seconds.

      This issue may be somehow related to issue 478 (https://issues.apache.org/jira/browse/HTTPCLIENT-478), but I couldn't find any help in any of the solutions there.
      I suspect the problem occurs when Java's InetSocketAddress constructor calls InetAddress.getByName(String) after DefaultProtocolSocketFactory.createSocket tries to create a new socket.

      Example code:
      --------------------

      import org.apache.commons.httpclient.HttpClient;
      import org.apache.commons.httpclient.methods.GetMethod;

      public class HttpConnectionTimeoutProblem
      {
      public static void main(String[] args)
      {
      long start = System.currentTimeMillis();
      try

      { HttpClient client = new HttpClient(); client.getParams().setConnectionManagerTimeout(1000); client.getParams().setSoTimeout(1000); GetMethod method = new GetMethod("http://www.987aksj239874nkjhse.gov"); method.getParams().setSoTimeout(1000); int httpRc = client.executeMethod(method); System.out.println("httpRc = " + httpRc); }

      catch (Exception e)

      { long time = System.currentTimeMillis() - start; System.out.println("Exception caught after " + time + ": " + e); e.printStackTrace(); }

      }
      }

      The output from this is (on my Linux machine)
      --------------------------------------------------------------
      log4j:WARN No appenders could be found for logger (org.apache.commons.httpclient.HttpClient).
      log4j:WARN Please initialize the log4j system properly.
      Exception caught after 20593: java.net.UnknownHostException: www.987aksj239874nkjhse.gov
      java.net.UnknownHostException: www.987aksj239874nkjhse.gov
      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
      at java.net.Socket.connect(Socket.java:520)
      at java.net.Socket.connect(Socket.java:470)
      at java.net.Socket.<init>(Socket.java:367)
      at java.net.Socket.<init>(Socket.java:240)
      at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:79)
      at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:121)
      at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:706)
      at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:386)
      at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:170)
      at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396)
      at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:324)
      at HttpConnectionTimeoutProblem.main(HttpConnectionTimeoutProblem.java:17)

      Thanks

      Attachments

        Activity

          People

            Unassigned Unassigned
            ozlevanon Oz Levanon
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: