Found out an interesting surprise today with the java.net.HttpURLConnection class.
Take a look at the two following code fragments:
URL url = new URL("http://localhost:8080/something/somewhere/");
URLConnection conn = url.openConnection();
HttpURLConnection c = (HttpURLConnection) conn;
System.out.println("1." + c.getInputStream());
System.out.println("2." + c.getLastModified());
System.out.println("3." + c.getContentType());
System.out.println("4." + c.getResponseCode());
and
URL url = new URL("http://localhost:8080/something/somewhere/");
URLConnection conn = url.openConnection();
HttpURLConnection c = (HttpURLConnection) conn;
System.out.println("2." + c.getLastModified());
System.out.println("3." + c.getContentType());
System.out.println("4." + c.getResponseCode());
System.out.println("1." + c.getInputStream());
(simple reordering of the call to getInputStream())
I expected both fragments to yield the same results, however there's a hidden surprise here - if the location you point your URL object to does not exist, then the first fragment will raise a FileNotFoundException (as expected), but the second one will give you an InputStream containing the error page from the server!
This certainly had me on a wild goose chase for an hour! :)
Update:
Ok, looks like this is a problem specific to JDK 1.3.1, JDK 1.4.x exhibits correct behaviour.
Posted by crafterm at June 27, 2003 07:07 PM | TrackBackHi Marcus, did you try both?
Posted by: Marc Portier at June 30, 2003 07:13 AMHi Marc,
Yes, gave both a test run on my Linux system here (JDK 1.3.1), why, do you get different results?
Cheers,
Marcus
Posted by: Marcus Crafter at June 30, 2003 10:57 AM