"java.io.FileNotFound " Exception while accessing a remote Server
Hi All
I'm facing a very Strange Exception in my java code.
The java file is making an HTTP connection with a remote server running on the Tomcat (server has same configuration as that on my machine...). My application sends a request in XML file to this server and receives the response in the XML file .
Now the problem is that the first two request are processed by the server correctly....but on the third request it gives the Exception:-
java.io.FileNotFoundException: <server-url>
The source of the errror is from this piece of code ==>
Code:
path = Config.getServerLocation(serviceId); // This gets the URL of the server
URL url = new java.net.URL(path);
con = (HttpURLConnection) url.openConnection();
con.setUseCaches(false);
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "text/xml");
OutputStream os = con.getOutputStream();
os.write(xml.getBytes());
con.connect();
int code = con.getResponseCode();
if (code == HttpURLConnection.HTTP_OK) {
InputStream is = con.getInputStream(); //This InputStream 'is' is the response from the server
return XMLHelper.loadXML(is);
}
The stackTrace says that the Exception is thrown from the getResponseCode() which is due to the exception thrown by the HttpURLConnection.java class's method getInputStream()
Is it a BUG in the HttpURLConnection.java for JDK1.3 ???? OR
Am I making some mistake ???
The environment I'm using to comile the code is JDK1.3
& the server is deployed on Tomcat 4.0.6 .
did you try to compile your code using jdk 1.4.x? just for testing sake, if it works then you know that there is a problem with the jdk support for that part of the code otherwise problem with your logic.
I assume that code is in a method called from somewhere else since there is only one request/response cycle there. You're not closing the connection when you're done so maybe you're hitting some arbitrary two connection limit on one end or the other.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." Brian W. Kernighan
U r correct that the method containing the code is called by other method which creates the Request XML and provide it to this function for sending this request to the Server.
But I'm using HTTP protocol for connecting to the server and according to my knowledge HTTP is Stateless protocol. It terminates the connection after the server responds to the request.So there is no possibility of having two connections at server.So this is not the problem...
I think the problem may be in HttpURLConnection.getResponseCode() method.... Since the exception's root cause is the statement for getting the response code from the server.
But how the hell first 2 request are getting served by the server ???
Rahul...
Last edited by rahu_webdev; 01-19-2005 at 11:13 PM.
You're probably right about the connection handling. I've always done it at the socket level so I'm not used to the higher level workings of the URLConnection. Are each of these three requests exactly the same? If not, what happens if you actually repeat the first request three times? Does it still fail consistently on the third try? You've definitely got a weird one there.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." Brian W. Kernighan
I've tried with sending the same request for multiple time.But it works.
I dont think there is any problem with the request since the same request works fine for the server when I debug the code through NetBeans . Also the requests aew well tested upon the server before implementing them in the code.
For my ammusement how the same code & requests works in the Netbeans ????
Note: Ray I'm closing the connection after the server responds to Request XML. I forgot to mention it earlier....
Is it due to the different JDK used by the server & on my local machine ???
Rahul
Last edited by rahu_webdev; 01-20-2005 at 01:29 AM.
there is one is one options here in this scenario.
I have noticed one thing missing and that is add a user agent type before you get the connection so that your code may look like this
[b]con.setDoOutput(true);
con.setDoInput(true);
connection.setRequestProperty( Gecko/20030312","User-Agent","Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.3);
In my opinion it will be last thing to suspect con.getResponseCode();
second you can put a delay between any 2 connections that will give your web browser a breather(though I am not sure if this is a problem).
Try the suggestion above...and if you still have problem then you may wanna show us more of your code then this snippet
Thanks for your valuable suggestions. But I solved the problem by using a differrent JDK than jdk1.3 (which i was using previously). I tried the JDK1.3.1_13 and to my surprise it makes it working.
Thanks for your addings..those are eally valuable one.
Bookmarks