124697
124697

Reputation: 21893

Why does this code throw a java.io.FileNotFoundException even when the url works fine

I am getting this error for this specific site (nandos.co.uk/southharrow). other urls work fine. Anyone know why?

java.io.FileNotFoundException: http://www.nandos.co.uk/southharrow at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)

public class Test {
    public static void main(String[] args) {
        HttpURLConnection conn = null;
        StringBuilder result = new StringBuilder();
        try {
            String website = "http://www.nandos.co.uk/southharrow";
            URL url = new URL(website.trim());
            conn = (HttpURLConnection) url.openConnection();
            InputStreamReader in = new InputStreamReader(conn.getInputStream());

            int read;
            char[] buff = new char[1024];
            while ((read = in.read(buff)) != -1) {
                result.append(buff, 0, read);
            }
        } catch (MalformedURLException e) {
            System.out.println("Error processing Places API URL");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("Error connecting to Places API");
            e.printStackTrace();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                conn.disconnect();
            }

        }
        System.out.println(result.toString());
    }

}

Upvotes: 0

Views: 525

Answers (2)

mongermd
mongermd

Reputation: 227

It appears that the URL is incorrect and returns a 404, which is interpreted as a FileNotFoundException:

> GET /southharrow HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8y zlib/1.2.5
> Host: www.nandos.co.uk
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Date: Wed, 29 Jan 2014 21:28:59 GMT
< Server: Apache
< X-Powered-By: PHP/5.3.27

You should verify the URL path again. Also, you may want to try using curl for quick testing.

Upvotes: 1

Pedantic
Pedantic

Reputation: 5022

$ wget http://www.nandos.co.uk/southharrow
--2014-01-29 16:28:31--  http://www.nandos.co.uk/southharrow
Resolving www.nandos.co.uk... 162.13.100.232
Connecting to www.nandos.co.uk|162.13.100.232|:80... connected.
HTTP request sent, awaiting response... 404 Not Found
2014-01-29 16:28:31 ERROR 404: Not Found.

No URL exists at that address. The page you see is a default page shown when there's an error. Similar to a "FileNotFoundException"

Upvotes: 4

Related Questions