FileNotFoundException with 404 status for valid UR

2019-04-13 05:32发布

问题:

I have the following code to perform a GET request on the following URL:

http://rt.hnnnglmbrg.de/server.php/someReferenceNumber

However, here is my output from Logcat:

java.io.FileNotFoundException: http://rt.hnnnglmbrg.de/server.php/6

Why does it return 404 when the URL is clearly valid?

Here is my connect code:

/**
 * Performs an HTTP GET request that returns base64 data from the server
 * 
 * @param ref
 *            The Accident's reference
 * @return The base64 data from the server.
 */
public static String performGet(String ref) {
    String returnRef = null;
    try {
        URL url = new URL(SERVER_URL + "/" + ref);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");

        BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));

        StringBuilder builder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }

        returnRef = builder.toString();

    } catch (IOException e) {
        e.printStackTrace();
    }
    return returnRef;
}

回答1:

When you request the URL, it actually return HTTP code 404 which mean not found. If you have control to the PHP script, set the header to 200 to indicate file is found.



回答2:

You are getting a 404, as said above. To avoid an exception, try something like this:

HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.connect () ; 
int code = con.getResponseCode() ;
if (code == HttpURLConnection.HTTP_NOT_FOUND)
{
    // Handle error
}
else
{
    BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));

    // etc...
}


回答3:

Never trust what you see in your browser. Always try to mimic your request using something like curl, and you'll clearly see that you're getting an HTTP 404 response code.

java.net will translate the HTTP 404 code to a FileNotFoundException

curl -v  http://rt.hnnnglmbrg.de/server.php/4
* About to connect() to rt.hnnnglmbrg.de port 80 (#0)
*   Trying 217.160.115.112... connected
* Connected to rt.hnnnglmbrg.de (217.160.115.112) port 80 (#0)
> GET /server.php/4 HTTP/1.1
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5
> Host: rt.hnnnglmbrg.de
> Accept: */*
> 
< HTTP/1.1 404 Not Found
< Date: Mon, 11 Jun 2012 07:34:55 GMT
< Server: Apache
< X-Powered-By: PHP/5.2.17
< Transfer-Encoding: chunked
< Content-Type: text/html
< 
* Connection #0 to host rt.hnnnglmbrg.de left intact
* Closing connection #0
0

From the javadocs at http://docs.oracle.com/javase/6/docs/api/java/net/HttpURLConnection.html

Returns the error stream if the connection failed but the server sent useful data nonetheless. The typical example is when an HTTP server responds with a 404, which will cause a FileNotFoundException to be thrown in connect, but the server sent an HTML help page with suggestions as to what to do.