URLConnection FileNotFoundException for non-standard HTTP port sources

I tried to use the Apache Ant Get task to get a list of the WSDL generated by another team in our company. They are hosted on the weblogic 9.x server at http: //....com: 7925 / services / . I can go to the page through a browser, but the get task gives me a FileNotFoundException when I try to copy the page to a local file for parsing. I could still get (using the Ant task) a URL without a non-standard port 80 for HTTP.

I looked at the Ant source code and narrowed the error to URLConnection. It seems that URLConnection does not recognize the data, it is HTTP traffic because it is not on the standard port, although the protocol is specified as HTTP. I sniffed the traffic with WireShark, and the page loaded correctly over the wire, but still gets a FileNotFoundException.

Here is an example where you will see an error (with a modified URL to protect the innocent). The error is called on connection.getInputStream ();

 import java.io.File; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; public class TestGet { private static URL source; public static void main(String[] args) { doGet(); } public static void doGet() { try { source = new URL("http", "test.com", 7925, "/services/index.html"); URLConnection connection = source.openConnection(); connection.connect(); InputStream is = connection.getInputStream(); } catch (Exception e) { System.err.println(e.toString()); } } } 
+23
java urlconnection ant
Jun 02 '09 at 20:08
source share
7 answers

check the response code returned by the server

+7
Jun 03 '09 at 3:19
source

The response to my HTTP request is returned with a status code of 404, which caused a FileNotFoundException when calling getInputStream (). I still wanted to read the body of the response, so I had to use another method: HttpURLConnection # getErrorStream () .

Here is the JavaDoc snippet for getErrorStream ():

It returns a stream of errors if the connection is unsuccessful, but the server is sent nonetheless, useful data. Typical For example, when the HTTP server responds with 404, which will throw a FileNotFoundException on the connection, but the server sent an HTML help page with suggestions on what to do.

Usage example:

 public static String httpGet(String url) { HttpURLConnection con = null; InputStream is = null; try { con = (HttpURLConnection) new URL(url).openConnection(); con.connect(); //4xx: client error, 5xx: server error. See: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html. boolean isError = con.getResponseCode() >= 400; //In HTTP error cases, HttpURLConnection only gives you the input stream via #getErrorStream(). is = isError ? con.getErrorStream() : con.getInputStream(); String contentEncoding = con.getContentEncoding() != null ? con.getContentEncoding() : "UTF-8"; return IOUtils.toString(is, contentEncoding); //Apache Commons IO } catch (Exception e) { throw new IllegalStateException(e); } finally { //Note: Closing the InputStream manually may be unnecessary, depending on the implementation of HttpURLConnection#disconnect(). Sun/Oracle implementation does close it for you in said method. if (is != null) { try { is.close(); } catch (IOException e) { throw new IllegalStateException(e); } } if (con != null) { con.disconnect(); } } } 
+44
Nov 09 '10 at 18:07
source

This is an old thread, but I had a similar problem, and I found a solution that is not listed here.

I got the page in a browser, but got 404 when I tried to access it through HttpURLConnection. The url I was trying to get contained the port number. When I tried this without a port number, I successfully received a dummy page through HttpURLConnection. So the problem was in the non-standard port.

I began to think that access was limited, and in a sense it was. My solution was that I needed to tell the User-Agent server and also specify the file types that I expect. I am trying to read a .json file, so I thought the file type might also be a necessary specification.

I added these lines and finally worked:

 httpConnection.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) "); httpConnection.setRequestProperty("Accept","*/*"); 
+19
Feb 16 2018-10-16
source

I know this is an old thread, but I found a solution not listed here anywhere.

I tried to pull json data from the J2EE servlet on port 8080, but was getting an error not found in the file. I was able to pull the same json data from a php server running on port 80.

It turns out that in the servlet I needed to change doGet to doPost.

Hope this helps someone.

+2
Oct 10 '12 at 12:17
source

I tried this locally - using the provided code - and I don't get a FileNotFoundException , unless the server returns a 404 status response.

Are you sure you are connecting to the web server you are going to connect to? Is it likely that you are connecting to another web server? (I note that the port number in the code does not match the port number in the link)

0
Jun 03 '09 at 1:58
source

I had a similar problem, but the reason seems to be different: here is the exception trace:

 java.io.FileNotFoundException: http://myhost1:8081/test/api?wait=1 at sun.reflect.GeneratedConstructorAccessor2.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1491) at java.security.AccessController.doPrivileged(Native Method) at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1485) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1139) at com.doitnext.loadmonger.HttpExecution.getBody(HttpExecution.java:85) at com.doitnext.loadmonger.HttpExecution.execute(HttpExecution.java:214) at com.doitnext.loadmonger.ClientWorker.run(ClientWorker.java:126) at java.lang.Thread.run(Thread.java:680) Caused by: java.io.FileNotFoundException: http://myhost1:8081/test/api?wait=1 at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1434) at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:379) at com.doitnext.loadmonger.HttpExecution.execute(HttpExecution.java:166) ... 2 more 

So it looks like just getting the response code will result in the url connecting to the GetInputStream call.

0
Nov 30
source

I know this is an old thread, but just noticed something on it, so I thought that I would just post it.

As Jessica mentions, this exception occurs when using a non-standard port.

This only happens when using DNS. If I use the IP number, I can specify the port number, and everything works fine.

0
Oct 10 '13 at 8:42 on
source



All Articles