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

HttpURLConnection#getInputStream() disconnects when receiving 401

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 8u11
    • core-libs
    • x86_64
    • linux_ubuntu

      A DESCRIPTION OF THE REQUEST :

      Request not to disconnect inputstream when receiving 401.

      sun.net.www.protocol.http.HttpURLConnection#getInputStream() closes the connection if receiving a 401 in streaming mode, resulting getErrorStream() returning null. Source code: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8-b132/sun/net/www/protocol/http/HttpURLConnection.java#1633

      if (respCode == HTTP_UNAUTHORIZED) {
                         if (streaming()) {
                              disconnectInternal();
                              throw new HttpRetryException (
                                  RETRY_MSG2, HTTP_UNAUTHORIZED);
                          }

      JUSTIFICATION :
      There are web servers that return meaningful error messages when declines an authentication request and we would like to parse the response body when receiving 401.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      try {
         InputStream content = conn.getInputStream();
         // ...
      } catch (IOException ioe) {
         InputStream content = conn.getErrorStream();
         // content should not be null.
      }
      ACTUAL -
      try {
         InputStream content = conn.getInputStream();
         // ...
      } catch (IOException ioe) {
         InputStream content = conn.getErrorStream();
         // content is null when getting a 401
      }

      CUSTOMER SUBMITTED WORKAROUND :
      There is no workaround right now because the error stream only gets set when calling getInputStream(), which always closes the connection when getting a 401 in streaming mode.

            michaelm Michael McMahon
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: