JDK-4191815 : HttpURLConnection shouldnt throw EOF exception for errors.
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 1.1.6,1.2.0,1.3.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic,solaris_2.6,windows_2000
  • CPU: generic,x86,sparc
  • Submitted: 1998-11-20
  • Updated: 2013-09-13
  • Resolved: 2001-01-26
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.4.0 betaFixed
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Description

Name: jn10789			Date: 11/20/98


Currently if you return an error, say 410 No Session,
to a HttpURLConnection and try and call getResponseCode
the first time you get an EOFException... If you call it again
you get the real correct answer, 410. This is kind of annoying.

Notice the second call in the code below:

    URLConnection urlConn=null;
    URL profileUrl = makeUrl(attr);
    urlConn = profileUrl.openConnection();
    urlConn.setDoOutput( true );
    urlConn.setDoInput( true );
    urlConn.setUseCaches( false );
    urlConn.setRequestProperty("Cookie", "sid=" + sessionId );
    try {
	responseCode = ((HttpURLConnection)urlConn).getResponseCode();
    } catch (FileNotFoundException ex) {
	responseCode = ((HttpURLConnection)urlConn).getResponseCode();
    }

Please note that this is related to bugid 4040926, which
seems to suggest that calling getResponseCode will return a 
valid response code not a EOFException.
(Review ID: 35747)
======================================================================

Name: ks88420			Date: 09/13/2000


twofish ~ > java -version
java version "1.3.0rc1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0rc1-b17)
Java HotSpot(TM) Client VM (build 1.3.0rc1-b17, mixed mode)


When an HttpURLConnection is used to connect to a URL that will return a 401
(authorization required) reply, calling getResponseCode() results in a
FileNotFoundException being thrown.

-----------------------------------------------------------------------------

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

public class Test2 {
  public static void main(String [] argv) {
    URLConnection u = null;
    HttpURLConnection c = null;
    try { u = (new URL(argv[0])).openConnection(); }
    catch (MalformedURLException e) { System.out.println("URL "+argv[0]+" is
malformed.");
                                      System.exit(0); }
    catch (IOException e) { System.out.println("IOException "+e+" opening
URL "+argv[0]);
                            System.exit(0); }
    c = (HttpURLConnection) u;
    try { c.connect(); } catch (IOException e) { System.out.println("Error
connecting; "+e);
                                                 System.exit(0); }
    int i = 999;
    try { i = c.getResponseCode(); } catch (IOException e) {
      System.out.println("getResponseCode failed first try; "+e);
    }
    try { i = c.getResponseCode(); } catch (IOException e) {
      System.out.println("getResponseCode failed second try; "+e);
      try { System.out.println(u.getContent().toString()); }
      catch (Exception e2) { System.out.println("Exception getting
content; "+e2);
                             System.exit(0);
      }
      System.exit(0);
    }
    System.out.println("Response code is "+i);
  }
}



----------------------------------------------------------------------------

twofish ~/pcaproto/pca/client > java -cp . Test2
http://www.cs.princeton.edu/csguide/software
getResponseCode failed first try; java.io.FileNotFoundException:
http://www.cs.princeton.edu/csguide/software
getResponseCode failed second try; java.io.FileNotFoundException:
http://www.cs.princeton.edu/csguide/software
Exception getting content; java.io.FileNotFoundException:
http://www.cs.princeton.edu/csguide/software
twofish ~/pcaproto/pca/client >


---------------------------------------------------------------------------
I've tried connecting to a dummy port so that I could see exactly what text the
HttpURLConnection sent to the server.  I then manually connected to a server to
make sure that the request was correctly responded to.  As you see, it was.


rayban ~/scratch/java > telnet www 80
Trying 128.112.136.11...
Connected to glia.
Escape character is '^]'.
GET /csguide/software HTTP/1.1
  User-Agent: Java1.3.0rc1
Host: www.cs.princeton.edu
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive

HTTP/1.1 401 Authorization Required
Date: Wed, 13 Sep 2000 19:21:41 GMT
Server: Apache/1.3.12 (Unix) mod_ssl/2.6.6 OpenSSL/0.9.5a PHP/4.0.2
AuthMySQL/2.20
WWW-Authenticate: Basic realm="CS Unix Password"
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=iso-8859-1

1da
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>401 Authorization Required</TITLE>
</HEAD><BODY>
<H1>Authorization Required</H1>
This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.<P>
<HR>
<ADDRESS>Apache/1.3.12 Server at www.cs.princeton.edu Port 80</ADDRESS>
</BODY></HTML>

0
(Review ID: 109594)
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: generic merlin-beta FIXED IN: merlin-beta INTEGRATED IN: merlin-beta
14-06-2004

WORK AROUND Name: jn10789 Date: 11/20/98 wrap calls to getResponseCode in a try/catch: try { responseCode = ((HttpURLConnection)urlConn).getResponseCode(); } catch (FileNotFoundException ex) { responseCode = ((HttpURLConnection)urlConn).getResponseCode(); } ======================================================================
11-06-2004

EVALUATION Bug report 4040926 was incorrectly closed as "not a bug". The reported behavior should have been considered a bug although this is somewhat of a corner case since normally the user will have already called getInputStream() which will have thrown the error. Instead, in the description and in the previous bug report, the call to getInputStream() was first happening inside getResponseMethod() or getResponseCode(). I don't think that calling getResponseCode() or getResponseMethod() should itself throw FileNotFoundException. This should be caught internally and the actual response code or method returned. The getResponseCode() and getResponseMethod() methods should only return IOException if they get actual connection level failure. They should not throw an exception of they successfuly connect to the server and get a reply even if the reply itself indicates an HTTP-level request denial. sharon.liu@eng 2000-02-29 The java.net.HttpURLConnection.getResponseCode() method calls getInputStream() to make sure that we've got headers. But the getInputStream() may throw exceptions in case there is an error. I think we should put the getInputStream in a "try" block and ignore IOExceptions thrown by the getInputStream(). --- This is fixed in merlin and thus it's possible to do the following :- int respCode = http.getResponseCode(); if (respCode >= 400) { in = http.getErrorStream(); } else { in = http.getInputStream(); } alan.bateman@ireland 2001-01-15
15-01-2001