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

HttpURLConnection should not return an input stream if the results Content-Lengt

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.3.0
    • core-libs
    • None
    • unknown
    • generic

      At the moment HttpURLConnection.getInputStream() returns
      a (real) input stream even if there is no entity body
      in the response ( Content-Length field in the header is zero).
      If you try to read from this stream the application will block
      until the server closes the socket.

      ---- Test case ----

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

      public class Test {
          public static void main (String args[]) {
              try {
                  URL url = new URL ("http://sunweb.ireland/");
                  HttpURLConnection urlc = (HttpURLConnection) url.openConnection ();

                  urlc.setRequestMethod ("OPTIONS");

                  int r = urlc.getResponseCode ();
                  System.out.println (r);
                  InputStream is = urlc.getInputStream ();
                  while ((r=is.read()) != -1) {
                      System.out.write (r);
                  }
              } catch (Exception e) {
                  e.printStackTrace ();
              }
          }
      }

            michaelm Michael McMahon
            michaelm Michael McMahon
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: