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.
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();
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,
--
This answer is courtesy of QuestionExchange.com
http://www.questionexchange.com/showUsenetGuest.jhtml?ans_id=6564&cus_id=USENET&qtn_id=6133
Regards,
Ken Kalish
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,