views:

1306

answers:

4

I was trying to use the Apache Ant coretask Get to get a list of WSDLs generated by another team in our company. They have them hosted on a weblogic 9.x server on http://....com:7925/services/. I am able to get to the page through a browser, but the get task gives me a FileNotFoundException when trying to copy the page to a local file to parse. I was still able to get (using the ant task) a URL without the non-standard port 80 for HTTP.

I looked through the Ant source code, and narrowed the error down to the URLConnection. It seems as though the URLConnection doesn't recognize the data is HTTP traffic, since it isn't on the standard port, even though the protocol is specified as HTTP. I sniffed the traffic using WireShark and the page loads correctly across the wire, but still gets the FileNotFoundException.

Here's an example where you will see the error (with the URL changed to protect the innocent). The error is thrown 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());
     }
    }

}
A: 

You need to set URLConnection.setDoInput(true) I believe.

Gandalf
blah I'm wrong, the default is true it seems.
Gandalf
A: 

I've tried that locally - using the code provided - and I don't get a FileNotFoundException except when the server returns a status 404 response.

Are you sure that you're connecting to the webserver you intend to be connecting to? Is there any chance you're connecting to a different webserver? (I note that the port number in the code doesn't match the port number in the link)

Daniel Martin
The server I am testing against gives me a 200 status code. The code example above doesn't have a valid URL because I didn't know of a public non-port 80 HTTP server.
jeffl8n
A: 

check the response code being returned by the server

objects
The server I am testing against gives me a 200 status code.
jeffl8n
How are you checking that?
objects
@objects I was checking it with Firefox. You are correct, though. The problem was the server is returning a 404 code to the Java code, but then displaying the index of WSDLs available and thus replying with a 200 OK status code to Firefox. I think it is the default behavior for XFire to display the listing of WSDLs whenever there's a 404 error.
jeffl8n
A: 

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

I was receiving the page fine in the browser, but got a 404 when I tried to access it via the HttpURLConnection. The URL I was trying to access contained a port number. When I tried it without the port number I successfully got a dummy page through the HttpURLConnection. So it seemed the non-standard port was the problem.

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

I added these lines and it finally worked:

httpConnection.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
httpConnection.setRequestProperty("Accept","[star]/[star]");
Jessica