10

I use a HttpURLConnection to connect to a website and receive an ResponseCode=404 (HTTP_NOT_FOUND). However I have no problem opening the website in my browser (IE).

Why the difference, and what can I do about it?

Regards, Pavan

This is my Program

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
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://localhost:8080/");

            System.out.println("Url is" + source.toString());

            URLConnection connection = source.openConnection();
            connection.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
            connection.setRequestProperty("Accept","*/*");
            connection.setDoInput(true);
            connection.setDoOutput(true);

            System.out.println(((HttpURLConnection) connection).getResponseCode());
            BufferedReader rdr = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            StringBuffer b = new StringBuffer();
            String line = null;
            while (true) {
                line = rdr.readLine();
                if (line == null)
                    break;
                b.append(line);
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.err.println(e.toString());
        }
    }

}

Stack Trace

Url ishttp://localhost:8080/
404
java.io.FileNotFoundException: http://localhost:8080/
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at TestGet.doGet(TestGet.java:28)
    at TestGet.main(TestGet.java:11)
Caused by: java.io.FileNotFoundException: http://localhost:8080/
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at java.net.HttpURLConnection.getResponseCode(Unknown Source)
    at TestGet.doGet(TestGet.java:26)
    ... 1 more
java.io.FileNotFoundException: http://localhost:8080/
Pavan Kumar
  • 211
  • 1
  • 5
  • 8

6 Answers6

15

You are getting 404 error that means the response for the request is not found. First you need to make sure that there is a server serving at http://localhost:8080/ and it must return some content with code 200. If not, then there is nothing we can help you.

The easiest way to test whether there is anything at the url is to paste the url on the web browser address bar and click go. However, this does not guarantee that the Java code will be able to access it. For example, if the server is designed to response 404 if it cannot find the web browser User-Agent header.

Since the server returns a status code, either 200 or 404, it means this is not a firewall problem.

According to your latest edition of the question, you can view it with the web browser but cannot download it with your java code and the header seems to be set correctly. There are only two problem I can see:

  1. You should not set connection.setDoOutput(true); to true. This will enforce the connection to do HTTP POST instead of GET and the server may not support POST.

  2. Your server may be always returning 404 even if it should have been 200. Since the web browser doesn't care about the error status and tries to render all the content so it seems to be working from the web browser. If so, you should fix the server to reponse correctly first, otherwise try getting error stream instead HttpURLConnection#getErrorStream()

Community
  • 1
  • 1
gigadot
  • 8,879
  • 7
  • 35
  • 51
2

I had a similar issue. For me it helped to inspect the packets using RawCap. RawCap is one of the few Windows packet sniffers that lets you sniff localhost.

In my cases the server was returning a 404 due to an authentication issue.

Iain
  • 10,814
  • 3
  • 36
  • 31
0

I know this is very late in the game, but I was just recently having the same issue and none of the solutions here worked for me. In my case, I actually had another process running on the same port that was stealing the requests from the java app. Using yair's answer here you can check for a process running on the same port like this: In the command prompt, do netstat -nao | find "8080" on Windows or netstat -nap | grep 8080 on Linux. It should show a line with LISTENING and 127.0.0.1:8080 and next would be the process ID. Just terminate the process and you should be good to go.

Community
  • 1
  • 1
jlars62
  • 7,183
  • 7
  • 39
  • 60
0

I had the problem too. In my case i had a invisible unicode character in the url string. So connection couldnt open it (FileNotFound indicates that). I removed it and it worked.

0

I had a similar scenario where the web service processed POST requests from the browser (in my case Postman, an API testing Chrome extension) correctly, but HttpURLConnection kept failing with a 404 for large payloads. I mistakenly assumed that the problem must be in my HttpURLConnection client code.

When I later tried to replicate the request from cUrl with a large payload, I got the same 404 error. Even though I used the cUrl code generated by Postman, which therefore should be identical to Postman's request, there was a difference in how the web service reacted to both requests. Some client middleware on Postman may have intercepted and modified the requests.

TL;DR

Check the web service. It may be the culprit. Try another non-browser barebones Http client like cUrl to see how the web service reacts to it.

Protongun
  • 3,230
  • 1
  • 15
  • 13
0

If the url http://localhost:8080/ can be accessed well in the web browser, the code should work well. I run the program in my machine, it works well. So you must check whether the webserver service is ok.

Andy Jiang
  • 23
  • 4