Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

HttpURLConnection can't see 404 Error

1,659 views
Skip to first unread message

swif...@hotmail.com

unread,
Oct 20, 1999, 3:00:00 AM10/20/99
to
Hello,

I am attempting to connect to URL's that don't exist
and expect to receive and verify a 404 error. I was
planning on using the
HttpConnection.getResponseCode() method.

I get a reference to the HttpConnection
HttpConnection httpConnection =
(HttpConnection)url.openConnection();


However, I get an java.io.FileNotFoundException when
using

httpConnection.getResponseCode()

Incidentally, I'm using VisualAge for Java so
HttpURLConnection.getResponseCode is calling
sun.net.www.protocol.http.HttpURLConection.getInputSt
ream() so this is probably why this is crashing.

So unfortunately, I cannot use this method. There
must be another way..ehh?

Thanks,
Scott Dickerson


Sent via Deja.com http://www.deja.com/
Before you buy.

Ken Kalish

unread,
Oct 20, 1999, 3:00:00 AM10/20/99
to
I really don't like the HttpURLConnection class because things occur behind the
scenes and often don't seem to follow any obvious logic. Sockets are better.

I have the same problem as you describe, using jdk 1.1.8 win95 here. It seems
you have to ask twice to get the answer. This example works. Here is the output:

java.io.FileNotFoundException: http://home.epix.net/~kjsdfsjf
Code = 0
Code = 404
Message = Not Found

Here is the code:

import java.net.*;

public class Http_ResponseCode {

static HttpURLConnection http;
static int x;

public static void main (String args [ ])
{
try
{
URL url = new URL ("http://www.epix.net/~kjsdfsjf");
http = (HttpURLConnection)url.openConnection() ;
http.connect();
//System.out.println(http.responseCode ); // can't get at variable,
//it's protected!
x = + http.getResponseCode(); //this throws the IOE
System.out.println("Code = " + x ); //never gets here because of
//IOE
}
catch (Exception e) { System.out.println(e); }
System.out.println("Code = " + x ); // was was not set 1st time
//try exact same thing again, it now works just fine
try {
x = + http.getResponseCode();
System.out.println("Code = " + x);
System.out.println("Message = " + http.getResponseMessage() );
}
catch (Exception e) { System.out.println(e); }
java.awt.Toolkit.getDefaultToolkit();

swift...@my-deja.com

unread,
Oct 27, 1999, 3:00:00 AM10/27/99
to
Well, for some strange reason (that makes me really uncomfortable)
something I changed allowed it to work, and unfortunately I didn't
version my code so I'm not sure what changed. I do know however that
this works now.

HttpConnection httpConnection =
(HttpConnection)url.openConnection();

int code=httpConnection.getResponseCode();
String message=httpConnection.getResponseMessage();

System.out.println("Response code: "+code+"Response
message: "+message);

Maybe the http.connect() call within your code messed up the state.
Many of these networking classes seem to be very aware of their state.
(Check the spec for HttpServletRequest.getWriter and
HttpServletRequest.getOutputStream.)

I was sent another comment about HttpURLConnection's and why Sockets
are often more reliable.

Eric Kaplan wrote:
>If you're writing an applet openConnection
>doesn't guaranty to return HTTP connection object. Running an applet
>from within Netscape openConnection returns a URLConnection object ...
>i.e. the returned object is dependent on JDK implementation ... FYI.

Luckily, I can be assured that since my testcase will always run on the
same JVM as an application that the openConnection will return an
HttpConnection.

Thanks for the advice,

QuestionExchange

unread,
Oct 28, 1999, 3:00:00 AM10/28/99
to
The JDK does indeed behave this way, and that's at
least arguably a bug - the expected behavior would be
that if the host address was valid but the page didn't
expect you'd get a 404 error. Of course, if the server
address itself wasn't valid, you wouldn't expect a 404 error,
because the 404 presupposes the existence of a server to
generate the 404 response.
But that's not how the JDK behaves.
What you need to do is to wrap your code in
a try/catch block:
try
{
System.out.println( "Trying " + args[0] );
java.net.URL u = new java.net.URL( args[0] );
java.net.HttpURLConnection h = (java.net.HttpURLConnection)
u.openConnection();
System.out.println( "Response code is " +
h.getResponseCode() );
}
catch( java.lang.Throwable t )
{
System.out.println( "we caught " + t.toString() );
// Handle invalid URL here
}
You distinguish the case of an invalid host name from
a valid host name with a page that doesn't exist by the
type of the exception: If the hostname is invalid
this code will catch an invalid host exception, while
if the hostname is valid but the URL doesn't name a real
resource it will (as you discovered) throw the
FileNotFound exception.

> I am attempting to connect to URL's that don't exist
> and expect to receive and verify a 404 error. I was
> planning on using the
> HttpConnection.getResponseCode() method.
>
> I get a reference to the HttpConnection
> HttpConnection httpConnection =
> (HttpConnection)url.openConnection();
>
>
> However, I get an java.io.FileNotFoundException when
> using
>
> httpConnection.getResponseCode()
>
> Incidentally, I'm using VisualAge for Java so
> HttpURLConnection.getResponseCode is calling
> sun.net.www.protocol.http.HttpURLConection.getInputSt
> ream() so this is probably why this is crashing.
>
> So unfortunately, I cannot use this method. There
> must be another way..ehh?

--
This answer is courtesy of QuestionExchange.com
http://www.questionexchange.com/showUsenetGuest.jhtml?ans_id=6564&cus_id=USENET&qtn_id=6133

Ken Kalish

unread,
Oct 28, 1999, 3:00:00 AM10/28/99
to
can you post the code that works? I eliminated the call to connect(), but it
behaves exactly as before, i.e. by still throwing the exception.

Regards,

Ken Kalish

evrb...@my-deja.com

unread,
Oct 29, 1999, 3:00:00 AM10/29/99
to
Finally, I found someone who's trying to work on the same problem that I
have!!
My purpose is to check whether the site exists or not, but I have used
sockets. I was not comfortable with HttpConnection. I have used the
'Head' method to get the validity of the page.

I work inside the GE domain. The program works for sites like
www.ge.com here. This is failing in cases of external websites like
www.yahoo.com. It is failing for some internal sites. The sites are
in the intranet)

Possible reasons for this could be that

the internal site must have a DNS entry.
For external sites, I must specify / modify some parameters such that
the proxy server allows this.
I would like to find out what these are and how they must be set.
For this , I am trying to get the Header information from a website
after opening a connection using a socket.

For some intranet sites (web05.corporate.ge.com), I am being able to
display the contents when I use the 'GET' method. But I get a server
error when I use a 'HEAD' method (as below). For most sites in the
intranet however, I get a 200 Ok message

***************

HTTP/1.1 500 Server Error

Date: Mon, 09 Aug 1999 12:17:11 GMT

Content-type: text/html

**************

I was told that this is possibly because the webserver may not support
the 'Head' method. You can get a word or two about this method at

http://java.sch.bme.hu/doc/java/javaunleashed/htm/ch26.htm (search for
'HEAD')

Becuase of the firewall set-up here and I go thru the proxy server. To
connect to external websites (ex. www.yahoo.com) I have given the foll.
command at the prompt , but neither of them worked.

* java -Dhttp.proxySet=True -Dhttp.proxyHost= proxyhostname
-Dhttp.proxyPort=80 classname

* java -Dhttp.firewallSet=True -Dhttp.firewallHost= proxyhostname
-Dhttp.firewallPort=80 classname

I can view these external sites from the browser.

Trying the command

java -DproxySet=true -Dhttp.proxyHost=http-proxy.corporate.ge.com
-DproxyPort=80 UrlValid "www.yahoo.com"

gives a "UnknownHostException".

Any help / documentation in this area will be a great help.

Thanks
Bhanu

Java application

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.net.*;

public class UrlValid
{
public static void main(String[] args) throws IOException {
Socket newSock = null;
//PrintWriter out = null;
DataOutputStream out = null;
BufferedReader in = null;

String Url = args[0];
String StrChek= "F";
try
{
newSock = new Socket(Url, 80);
System.out.println("echo: " + Url);
out = new DataOutputStream(newSock.getOutputStream() );
in = new BufferedReader(new
InputStreamReader(newSock.getInputStream()));
}
catch (UnknownHostException e)
{
System.out.println("Don't know about host: " + e);
StrChek= "T";
}

BufferedReader stdIn = null;
if ( StrChek.equals("F"))
{
try
{
out.writeBytes("HEAD / HTTP/1.0 \r\n\r\n");
stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
do
{
System.out.println("echo: " + in.readLine());
}
while (in.readLine() != null);

}
catch (IOException e)
{
System.err.println("Couldn't get I/O the connection.");
}
out.close();
in.close();
stdIn.close();
newSock.close();
}
}
}

In article <7ul3ra$jef$1...@nnrp1.deja.com>,
swif...@hotmail.com wrote:
> Hello,


>
> I am attempting to connect to URL's that don't exist
> and expect to receive and verify a 404 error. I was
> planning on using the
> HttpConnection.getResponseCode() method.
>
> I get a reference to the HttpConnection
> HttpConnection httpConnection =
> (HttpConnection)url.openConnection();
>
> However, I get an java.io.FileNotFoundException when
> using
>
> httpConnection.getResponseCode()
>
> Incidentally, I'm using VisualAge for Java so
> HttpURLConnection.getResponseCode is calling
> sun.net.www.protocol.http.HttpURLConection.getInputSt
> ream() so this is probably why this is crashing.
>
> So unfortunately, I cannot use this method. There
> must be another way..ehh?
>

> Thanks,

0 new messages