Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-2138360 | 5.0u8 | Unassigned | P5 | Closed | Cannot Reproduce | |
JDK-2021970 | 1.4.0 | Yingxian Wang | P5 | Resolved | Fixed | merlin |
JDK-2021969 | 1.3.1_01 | Yingxian Wang | P5 | Closed | Fixed | 01 |
The RFE 4087140 which was approved by the CCC describes the method getErrorStream() which would provide an error stream in the case of a FileNotFoundException. Probably the best course would be to always throw the FileNotFoundException on >400 responses for any type of document. Applications could then catch this exception and call getErrorStream() if they wanted access to the error stream (as in the case of HotJava, where it displays the server error message)
Name: krT82822 Date: 09/29/99
The implementation of java.net.HttpURLConnection.getErrorStream() is "return null". Furthermore, the implementation of sun.net.www.protocol.http.HttpURLConnection.getErrorStream() is also "return null".
The fact that these methods are unimplemented indicates that they will never work as defined.
This is a major problem, becuase of the behaivor of the getInputStream() methods on the same classes. These methods throw a FileNotFoundException if the HTTP response code is >= 400. Nearly every web server in existence returns a document along with an error code -- for example, if the error code is 404, the document usually says "Document not found", and generally includes the e-mail address of the webmaster, and possibly other useful information. Sometimes this document even indicates that a search returned no matches. In any case, it is often important that this "error" document is made available to the user -- but there is NO WAY to get this document using the URLConnection classes.
I'm trying to write an HTTP proxy in Java. I CANNOT do so, however, becuase I can't handle error conditions -- which, of course, happen all the time on the Internet. The end user's browser gets a "document contained no data" or similar error instead of the real error document that the remote web server created.
import java.io.*;
import java.net.*;
public class Connect {
static byte[] buf;
static InputStream is, es;
static URLConnection conn;
public static void main(String[] args) {
buf = new byte[80];
try{
URL url = new URL("http://java.sun.com/products/jdk/1.3/docs/badLastSeg");
conn = url.openConnection();
is = conn.getInputStream();
es = ((HttpURLConnection)conn).getErrorStream();
System.out.println("input stream IS:" + is);
System.out.println("error stream IS:" + es);
is.read(buf);
System.out.println("buf is:" + buf);
for(int i=0; i < buf.length; i++) {
System.out.print(buf[i]);
}
System.out.println("-----");
}
catch(Exception e){
System.err.println("YIKES!");
System.out.println(e);
try {
System.out.println("err stream is:" + es);
// commence workaround... (which doesn't work yet)
es = ((HttpURLConnection)conn).getErrorStream();
System.out.println(
"err stream (after 2nd getErrorStream()) is (still) : " + es);
if(es != null) {
es.read(buf);
for(int i=0; i < buf.length; i++) {
System.out.print(buf[i]);
}
}
}
catch(Exception e2) {
System.exit(1);
}
System.exit(1);
}
}
}
(Review ID: 95930)
======================================================================
Name: krT82822 Date: 12/03/99
C:\Ian\demo>c:\jdk1.2.2\bin\java -version
java version "1.2.2"
Classic VM (build JDK-1.2.2-001, native threads, symcjit)
C:\Ian\demo>
Two attachments, as it were, here. First the stack trace and second the test
program. Use this test program to surf to a cite that gives an unauthorized
response (401). I'm sure you can find a site like that (ours is from an
internal servlet, you don't really want that too). Try the test program with a
site that returns 200, 301... anything
less that 400 and everything is okay. Try anything 400 and over and you get
a file not found exception and 404 is NOT the only over 400 code. 401 should
be returned!
LoginTest.java is a complete program just run it from the command line.
**********************Stack Trace
C:\Ian\demo>rl
C:\Ian\demo>c:\jdk1.2.2\bin\java LoginTest
java.io.FileNotFoundException: http://192.168.0.129/servlet/Demo1.LoginHandler.L
oginHandler
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLCon
nection.java:530)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:145
)
at LoginTest.main(LoginTest.java:14)
C:\Ian\demo>rl
C:\Ian\demo>c:\jdk1.2.2\bin\java LoginTest
204
No Content
************* end stack trace
*******************************file LoginTest.java
class LoginTest
{
public static final void main(String args[])
{
try
{
java.net.URL url = new
java.net.URL("http://192.168.0.129/servlet/Demo1.LoginHandler.LoginHandler");
java.net.HttpURLConnection hurlc =
(java.net.HttpURLConnection)url.openConnection();
hurlc.setDoOutput(true);
hurlc.setDoInput(true);
java.io.OutputStream out = hurlc.getOutputStream();
out.write("name=micky&passwd=mouse".getBytes());
System.out.println(hurlc.getResponseCode());
System.out.println(hurlc.getResponseMessage());
java.io.InputStream in = hurlc.getInputStream();
byte[] b = new byte[2000];
in.read(b);
System.out.println(new String(b));
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
*********** end file LoginTest.java
(Review ID: 98576)
======================================================================
- backported by
-
JDK-2021970 sun.net.www.protocol.http.HttpURLConnection error handling
- Resolved
-
JDK-2021969 sun.net.www.protocol.http.HttpURLConnection error handling
- Closed
-
JDK-2138360 sun.net.www.protocol.http.HttpURLConnection error handling
- Closed
- duplicates
-
JDK-4150792 HttpURLConnection should not throw a FileNotFoundException on HTTP 401 response
- Closed
-
JDK-4492994 HttpURLConnection generates erroneous FileNotFoundException if respCode >=400
- Closed
-
JDK-4222009 file not found template is shown instead of host not found reply
- Closed
-
JDK-4655826 Can't use HttpURLConnection.getResponseCode to obtain response code >=400
- Closed
-
JDK-4191207 URLConnection.getContent() doesn't throw FileNotFoundException
- Closed
-
JDK-4300174 Wrong line delimiters in DataOutputStream of HttpURLConnection
- Closed
-
JDK-4314717 HttpURLConnection get error message from server response not hooked up
- Closed
-
JDK-4406592 HttpURLConnection fails on some valid URLs with FileNotFoundException
- Closed
- relates to
-
JDK-4650090 getResponseCode()
- Resolved
-
JDK-4523989 Behavior of getResponseCode() is different between 131_01 and
- Resolved