Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4330985

If URLConnection fails to getInputStream on first try, it never connects again

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.2.2
    • core-libs
    • x86
    • windows_nt



      Name: stC104175 Date: 04/17/2000


      java version "1.2.2"
      Classic VM (build JDK-1.2.2-001, native threads, symcjit)

      I'm having an odd problem trying to gracefully handle network problems with URL
      connections (actually HTTP connections) within a java application (running
      standalone -- not an applet and not running in a browser).

      I'm looping on a call to an HTTP server. I want to get the HTML and continue to
      update my java app with information from the retrieved HTML. I can open streams
      to the HTTP server and get the response. Not a problem. I even handle cases
      where calling getInputStream() on a URLConnection throws an IOException due to
      network failure. In this case, I continue to loop until the network becomes
      available. When it comes back up, the getInputStream() eventually works and
      everything is fine.

      Here's the problem... this only works if the **FIRST** time I call
      getInputStream(), it succeeds! If the network is down the **FIRST** time I call
      getInputStream(), every subsequent call to getInputStream() fails, regardless
      of whether or not the network comes back up!

        To see what was going on, I wrote a couple of test classes. They try to connect
      to the original server. If this fails (due to a network problem) they sleep
      long enough for me to bring the network back up (plugging the cable back in)
      and then they try to connect to a *Different* server. If the network is up by
      then, the connection to the second server will succeed, while the connection to
      the first server will continue to fail on every subsequent call!
        To get the communication to work, I have to quit my app and restart it. Then,
      if the *First* try at connecting works, everything is fine. Plus, it doesn't
      matter what server I try - this happens with every server I have tried.

      The first time it fails, it hangs for about 20-30 seconds trying to connect
      before throwing the exception. Every subsequent failure returns instantly with
      an IOException error! It looks as if it is caching the fact that the server
      wasn't found and thus, never trys to connect to it again?! I've tried
      setUseCaches(false) with no change in behavior.

      I have yet to find a workaround or a way to turn this caching off (if this is
      really what is happening). I posted to the JDC and to Earthweb and have
      received no help. So, I searched for a workaround and looking at the source
      for InetAddress, it uses an undocumented class sun.net.InetAddressCachePolicy.
      I was unable to find documentation or source for InetAddressCachePolicy, so I
      decompiled it. It has two settings FOREVER and NEVER. However, the System
      class is setting it to FOREVER in a way that I can't override.
      InetAddressCachePolicy has a function setIfNotSet that is a public static
      function. However, using it throws a SecurityException with a message "can't
      make InetAddress cache more lax", since it was already set to FOREVER
      (determined by using InetAddressCachePolicy.get(), and comparing the returned
      value to InetAddressCachePolicy.NEVER and InetAddressCachePolicy.FOREVER). I
      don't even know if this is where the problem lies, or if there is a simple
      workaround to my problem, but I've looked everywhere.

      Below is the test code I'm using to display retrieved HTML from a URL:

      -----------------------------
      import java.io.*;
      import java.net.*;

      <<Rest of class ommitted>>

      public void DisplayURL() throws Exception
      {
        URL url = new URL("http://www.sun.com");
        URLConnection conn = url.openConnection();
        conn.setUseCaches(false);
        conn.connect();
        BufferedReader in = new BufferedReader(new InputStreamReader
      (conn.getInputStream()));

        String inputLine = "";
       
        while ((inputLine = in.readLine()) != null)
          System.out.println(inputLine + "\n");

        in.close();
      }
      -----------------------------

      Also, this is a major problem for our application. If there is a known
      workaround, or a solution that I have missed, PLEASE let me know.

      Any help would be appreciated.

      Thank you for your time.

      Anthony Glaviano
      ###@###.###
      (Review ID: 103668)
      ======================================================================

            alanb Alan Bateman
            stompkinsunw Sean Tompkins (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: