FileNotFoundException when receiving an InputStream from HttpURLConnection

I am trying to send a mail request to a URL using HttpURLConnection (for using cUrl in java). The content of the request is xml, and at the endpoint, the application processes the xml and stores the record in the database, and then sends the response as an xml string. The application is hosted locally on apache-tomcat.

When I execute this code from the terminal, the line is added to db, as expected. But the exception is thrown as follows, getting an InputStream from the connection

java.io.FileNotFoundException: http://localhost:8080/myapp/service/generate at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1401) at org.kodeplay.helloworld.HttpCurl.main(HttpCurl.java:30) 

Here is the code

 public class HttpCurl { public static void main(String [] args) { HttpURLConnection con; try { con = (HttpURLConnection) new URL("http://localhost:8080/myapp/service/generate").openConnection(); con.setRequestMethod("POST"); con.setDoOutput(true); con.setDoInput(true); File xmlFile = new File("test.xml"); String xml = ReadWriteTextFile.getContents(xmlFile); con.getOutputStream().write(xml.getBytes("UTF-8")); InputStream response = con.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(response)); for (String line ; (line = reader.readLine()) != null;) { System.out.println(line); } reader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } 

This is confusing because the exception is traced to the line InputStream response = con.getInputStream(); , and there seems to be no file to throw a FileNotFoundException.

When I try to open a connection to an xml file directly, it does not throw this exception.

The service application uses the spring framework and Jaxb2Marshaller to create an xml response.

The ReadWriteTextFile class is taken from here.

Thank.

Edit: Well, this saves the data in the database and at the same time sends back a 404 response status code.

I also tried to make curl using php and print out CURLINFO_HTTP_CODE , which would turn out to be 200.

Any ideas on how I can debug this? Both services and the client are on the local server.

Solved: I could solve the problem by contacting the answer to SO itself.

It seems that HttpURLConnection always returns a 404 response when connected to a non-standard port url.

Adding these lines resolved it

 con.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) "); con.setRequestProperty("Accept","*/*"); 
+75
java inputstream filenotfoundexception
Mar 21 2018-11-21T00:
source share
7 answers

I don't know about your Spring / JAXB combination, but the average REST web service will not return the response body to POST / PUT, just the status of the response . You want to define it instead of a body.

Replace

 InputStream response = con.getInputStream(); 

by

 int status = con.getResponseCode(); 

All available status codes and their meaning are available in the HTTP specification, as mentioned earlier. The web service itself should also contain some documentation that looks at all status codes supported by webservice, and their special meaning, if any.

If the status starts with 4nn or 5nn , you would like to use getErrorStream() instead to read the response body, which may contain error information.

 InputStream error = con.getErrorStream(); 
+99
Mar 21 2018-11-21T00:
source

FileNotFound is simply an unsuccessful exception used to indicate that the web server returned 404.

+38
Mar 21 2018-11-21T00:
source

Anyone with this problem in the future, the reason is that the status code was 404 (or in my case was 500). It seems that the InpuStream function will InpuStream error if the status code is not 200.

In my case, I manage my own server and return a 500 status code to indicate an error has occurred. Despite the fact that I also sent the body with a string message with a detailed description of the error, inputstream chose the error, despite the fact that the body is completely readable.

If you manage your server, I believe that this can be handled by sending yourself a status code of 200, and then processing any response to the string response.

+18
May 25 '14 at 17:09
source

For someone else stumbling over this, the same thing happened to me when I tried to send the SOAP request header to the SOAP service. The problem was the wrong order in the code, I asked for the input stream first before sending the XML body. In the code below, the string InputStream in = conn.getInputStream(); appeared immediately after ByteArrayOutputStream out = new ByteArrayOutputStream(); which is the wrong order of things.

 ByteArrayOutputStream out = new ByteArrayOutputStream(); // send SOAP request as part of HTTP body byte[] data = request.getHttpBody().getBytes("UTF-8"); conn.getOutputStream().write(data); if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) { Log.d(TAG, "http response code is " + conn.getResponseCode()); return null; } InputStream in = conn.getInputStream(); 

FileNotFound in this case was an unsuccessful way to encode an HTTP 400 response code.

+3
Mar 10 '14 at 15:24
source

FileNotFound in this case means that you received 404 from your server - maybe the server does not like POST requests?

+2
Mar 21 '11 at 14:45
source

Decision:
just change localhost to the IP of your PC.
if you want to know this: Windows + r> cmd> ipconfig
Example: http: // 192.168.0.107 /directory/service/program.php?action=sendSomething
just replace 192.168.0.107 with your own IP address (do not try 127.0.0.1 because it is the same as localhost )

0
Aug 13 '16 at 19:29
source

Change

 con = (HttpURLConnection) new URL("http://localhost:8080/myapp/service/generate").openConnection(); 

For

 con = (HttpURLConnection) new URL("http://YOUR_IP:8080/myapp/service/generate").openConnection(); 
-2
Jan 29 '16 at 9:32
source



All Articles