40N FileNotFoundException for a valid url request HTTP GET

I have the following code to execute a GET request at the following URL:

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

However, here is my conclusion from Logcat:

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

Why does it return 404 when the url is explicitly valid?

Here is my connection code:

/**
 * Performs an HTTP GET request that returns base64 data from the server
 * 
 * @param ref
 *            The Accident 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;
}
+2
source share
3 answers

When you request a URL, it actually returns an HTTP code 404, which means that it was not found. If you have control over a PHP script, set the header to 200to indicate that the file is found.

enter image description here

+4
source

404, . , - :

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...
}
+1

, . , - , , HTTP 404.

java.net HTTP 404 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

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

, , . , HTTP- 404, FileNotFoundException , HTML , .

0

All Articles