Java HTTP GET请求上有效URL的状态为404的FileNotFoundException
我有以下代码来对以下URL执行GET请求:Java HTTP GET请求上有效URL的状态为404的FileNotFoundException,java,android,http,Java,Android,Http,我有以下代码来对以下URL执行GET请求: http://rt.hnnnglmbrg.de/server.php/someReferenceNumber 但是,以下是我从Logcat获得的输出: java.io.FileNotFoundException: http://rt.hnnnglmbrg.de/server.php/6 当URL明显有效时,为什么返回404? 这是我的连接代码: /** * Performs an HTTP GET request that returns bas
http://rt.hnnnglmbrg.de/server.php/someReferenceNumber
但是,以下是我从Logcat获得的输出:
java.io.FileNotFoundException: http://rt.hnnnglmbrg.de/server.php/6
当URL明显有效时,为什么返回404?
这是我的连接代码:
/**
* 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;
}
当您请求URL时,它实际上返回HTTP code
404
,这意味着找不到。如果您可以控制PHP脚本,请将标题设置为200
,以指示找到文件
永远不要相信你在浏览器中看到的东西。始终尝试使用curl之类的东西来模拟您的请求,您将清楚地看到您得到的是HTTP404响应代码 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
curl-vhttp://rt.hnnnglmbrg.de/server.php/4
*即将连接()到rt.hnnnglmbrg.de端口80(#0)
*正在尝试217.160.115.112。。。有联系的
*连接到rt.hnnnglmbrg.de(217.160.115.112)端口80(#0)
>GET/server.php/4 HTTP/1.1
>用户代理:curl/7.21.4(universal-apple-darwin11.0)libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5
>主持人:rt.hnnnglmbrg.de
>接受:*/*
>
未找到
来自javadocs
如果连接失败,但服务器仍发送了有用的数据,则返回错误流。典型的例子是HTTP服务器响应404,这将导致在connect中抛出FileNotFoundException,但服务器发送了一个HTML帮助页面,其中包含关于如何操作的建议。如上所述,您将得到一个
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...
}
您是否在桌面浏览器中使用任何参考号进行了测试。这将提供服务器的任何详细信息。是的,它返回的很好。404不是关于url是否有效,而是关于找不到url,这是由http服务器响应的。但是如果它在我的桌面浏览器中工作,怎么可能找不到呢?@paranoid android例外是因为响应代码。你可以通过返回status
200
的URL来测试它。你是如何测试它的?Pau Kiat Wee绝对正确。正在生成的异常是由于url处的状态代码错误+1这不能解决问题,只能改变它。没有答案。