JDK-4523989 : Behavior of getResponseCode() is different between 131_01 and
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 1.3.1,1.3.1_01
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: solaris_8,windows_nt
  • CPU: x86,sparc
  • Submitted: 2001-11-07
  • Updated: 2009-06-25
  • Resolved: 2002-02-21
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.3.1_03 03Fixed
Related Reports
Duplicate :  
Relates :  
Description
Licensee reported us that behavior of HttpURLConnection.getResponseCode() is
different between 1.3.1 and 1.3.1_01. It seems to happen after implementation of
#4160499. This problem appears on Solaris and Windows.

- behavior of getResponseCode() included in JDK1.3.1 older
  When HTTP server returns HTTP responce, Java receive the responce code as
  server  returned.


- behavior of getResponseCode() included in JDK1.3.1_01
  When HTTP server returns code over 400(?), Java throw the exception. Detail of
  exception is uncertain.

We tested this matter by using followin sample code on Windows. In the case of
access to exsiting server, difference donesn't appear. But in the other case, some
differences appear.
----------------------
E:\>java -fullversion
java full version "1.3.1-b24"

E:\>java HttpTest GET http://jle   <- existing server
method=GET
URL=http://jle.japan.sun.com
ResponseCode=200

E:\>java HttpTest PUT http://jle
method=PUT
URL=http://jle.japan.sun.com
java.io.FileNotFoundException: http://jle
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:574)
        at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:230)
        at HttpTest.main(HttpTest.java:29)

E:\>java HttpTest DELETE http://jle
method=DELETE
URL=http://jle
java.io.FileNotFoundException: http://jle
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:574)
        at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:230)
        at HttpTest.main(HttpTest.java:29)

E:\>java HttpTest GET http://foo  <- not existing server
method=GET
URL=http://foo
java.net.UnknownHostException: foo
        at java.net.InetAddress.getAllByName0(InetAddress.java:571)
        at java.net.InetAddress.getAllByName0(InetAddress.java:540)
        at java.net.InetAddress.getByName(InetAddress.java:449)
        at java.net.Socket.<init>(Socket.java:100)
        at sun.net.NetworkClient.doConnect(NetworkClient.java:50)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:331)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:517)
        at sun.net.www.http.HttpClient.<init>(HttpClient.java:267)
        at sun.net.www.http.HttpClient.<init>(HttpClient.java:277)
        at sun.net.www.http.HttpClient.New(HttpClient.java:289)
        at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:408)

        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.ja
va:501)
        at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:230)

----------------------
E:\>java -fullversion
java full version "1.3.1_01"

E:\>java HttpTest GET http://jle
method=GET
URL=http://jle.japan.sun.com
ResponseCode=200

E:\>java HttpTest put http://jle
method=put
URL=http://jle
java.net.ProtocolException: Invalid HTTP method: put
        at java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:201)
        at HttpTest.main(HttpTest.java:28)

E:\>java HttpTest DELETE http://jle
method=DELETE
URL=http://jle
java.io.IOException: Server returned HTTP response code: 405 for URL: http://jle
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:564)
        at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:230)
        at HttpTest.main(HttpTest.java:29)

E:\>java HttpTest GET http://foo
method=GET
URL=http://foo
java.net.UnknownHostException: foo
        at java.net.InetAddress.getAllByName0(InetAddress.java:571)
        at java.net.InetAddress.getAllByName0(InetAddress.java:540)
        at java.net.InetAddress.getByName(InetAddress.java:449)
        at java.net.Socket.<init>(Socket.java:100)
        at sun.net.NetworkClient.doConnect(NetworkClient.java:50)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:335)
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:521)
        at sun.net.www.http.HttpClient.<init>(HttpClient.java:271)
        at sun.net.www.http.HttpClient.<init>(HttpClient.java:281)
        at sun.net.www.http.HttpClient.New(HttpClient.java:293)
        at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:404)

        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:497)
        at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:230)
        at HttpTest.main(HttpTest.java:29)



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

public class HttpTest {

  public static void main( String args[] ){
    URL url=null;
    HttpURLConnection con=null;

    if(args.length != 2){
      System.out.println("usage: java HttpTest method URL");
      System.exit(1);
    }

    System.out.println("method=" + args[0]);
    System.out.println("URL=" + args[1]);

    String method = new String(args[0]);
    try{
      url = new URL(args[1]);
    } catch(java.net.MalformedURLException ex){
      ex.printStackTrace();
      System.exit(1);
    }

    try{
      con = (HttpURLConnection)url.openConnection();
      con.setRequestMethod(method);
      int code = con.getResponseCode();
      System.out.println("ResponseCode=" + code);
    } catch(java.net.ProtocolException ex){
      ex.printStackTrace();
    } catch(java.io.IOException ex){
      ex.printStackTrace();
    }
  }
}
---->8----------------->8---------------->8-------------

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: 1.3.1_03 FIXED IN: 1.3.1_03 INTEGRATED IN: 1.3.1_03
14-06-2004

SUGGESTED FIX (sun.net.www.protocol.http) --- HttpURLConnection.java Wed Jan 30 04:14:38 2002 *** 1,7 **** /* ! * @(#)HttpURLConnection.java 1.82 01/12/21 * * Copyright 1995-2001 Sun Microsystems, Inc. All Rights Reserved. * * This software is the proprietary information of Sun Microsystems, Inc. * Use is subject to license terms. --- 1,7 ---- /* ! * @(#)HttpURLConnection.java 1.83 02/01/30 * * Copyright 1995-2001 Sun Microsystems, Inc. All Rights Reserved. * * This software is the proprietary information of Sun Microsystems, Inc. * Use is subject to license terms. *** 480,490 **** if (!doInput) { throw new ProtocolException("Cannot read from URLConnection" + " if doInput=false (call setDoInput(true))"); } ! if (inputStream != null && !connected && rememberedException != null) { if (rememberedException instanceof RuntimeException) throw (RuntimeException)rememberedException; else throw (IOException)rememberedException; } --- 480,490 ---- if (!doInput) { throw new ProtocolException("Cannot read from URLConnection" + " if doInput=false (call setDoInput(true))"); } ! if (rememberedException != null) { if (rememberedException instanceof RuntimeException) throw (RuntimeException)rememberedException; else throw (IOException)rememberedException; } *** 832,841 **** --- 832,873 ---- getInputStream(); } catch (IOException e) {} return responses.getValue(n); } + /** + * Overwrite super class method + */ + public int getResponseCode() throws IOException { + if (responseCode != -1) { + return responseCode; + } + + // this statement will call getInputStream + String resp = getHeaderField(0); + /* should have no leading/trailing LWS + * expedite the typical case by assuming it has + * form "HTTP/1.x <WS> 2XX <mumble>" + */ + int ind; + try { + ind = resp.indexOf(' '); + while(resp.charAt(ind) == ' ') + ind++; + responseCode = Integer.parseInt(resp.substring(ind, ind + 3)); + responseMessage = resp.substring(ind + 4).trim(); + return responseCode; + } catch (Exception e) { + try { + getInputStream(); + } catch (IOException ioe) { + throw ioe; + } + return responseCode; + } + } + /** * Gets a header field by index. Returns null if not known. * @param n the index of the header field */ public String getHeaderFieldKey(int n) { (java.net) --- HttpURLConnection.java Tue Jan 29 09:44:19 2002 *** 1,7 **** /* ! * @(#)HttpURLConnection.java 1.25 00/02/02 * * Copyright 1996-2000 Sun Microsystems, Inc. All Rights Reserved. * * This software is the proprietary information of Sun Microsystems, Inc. * Use is subject to license terms. --- 1,7 ---- /* ! * @(#)HttpURLConnection.java 1.26 02/01/29 * * Copyright 1996-2000 Sun Microsystems, Inc. All Rights Reserved. * * This software is the proprietary information of Sun Microsystems, Inc. * Use is subject to license terms. *** 221,254 **** * from the response (i.e., the response is not valid HTTP). * @throws IOException if an error occurred connecting to the server. * @return the HTTP Status-Code */ public int getResponseCode() throws IOException { ! if (responseCode != -1) { ! return responseCode; } - // make sure we've gotten the headers - getInputStream(); - String resp = getHeaderField(0); - /* should have no leading/trailing LWS - * expedite the typical case by assuming it has - * form "HTTP/1.x <WS> 2XX <mumble>" - */ - int ind; - try { - ind = resp.indexOf(' '); - while(resp.charAt(ind) == ' ') - ind++; - responseCode = Integer.parseInt(resp.substring(ind, ind + 3)); - responseMessage = resp.substring(ind + 4).trim(); - return responseCode; - } catch (Exception e) { - return responseCode; - } - } - /** * Gets the HTTP response message, if any, returned along with the * response code from a server. From responses like: * <PRE> * HTTP/1.0 200 OK --- 221,233 ---- * from the response (i.e., the response is not valid HTTP). * @throws IOException if an error occurred connecting to the server. * @return the HTTP Status-Code */ public int getResponseCode() throws IOException { ! return -1; } /** * Gets the HTTP response message, if any, returned along with the * response code from a server. From responses like: * <PRE> * HTTP/1.0 200 OK
11-06-2004

EVALUATION The behaviour of HttpURLConnection was changed for merlin. Specifically an IOException is now thrown for all http errors and a more specific FNF is thrown for "not found" errors (400 and 410). In addition the getErrorStream method was plumbed in so that the error page could be processed by the application. This behaviour change was intended for a major release (1.4) and was not intended for a patch release (1.3.1_01). ###@###.### 2001-11-07
07-11-2001