我在 java Jersey REST 中开发了一个 Web 服务。我使用 HttpClient 调用基本的 GET 方法。
在浏览器上使用此 URL http://localhost:8080/myserver/rest/location
时,它可以工作。
当使用此代码调用 GET 方法时,我收到 404 Not Found 错误。
private static final String BASIC_URL = "http://localhost:8080/myserver/rest/location";
public static boolean isAlive() throws URISyntaxException, ClientProtocolException, IOException{
HttpClient httpclient = new DefaultHttpClient();
HttpGet get=new HttpGet(BASIC_URL);
System.out.println(get.getURI());
System.out.println("after adding all namevalues: "+get.getURI());
HttpResponse response = httpclient.execute(get);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() != 200) {
System.out.println("response status="+statusLine.getStatusCode());
return false;
}
System.out.println("Response sent succesfully with status "+200);
return true;
}
有什么线索吗?
更新
另外,我尝试了http://192.168.9.160:8080/LocationRetreiverServer/rest/location
(我的本地IP),但它给出了“未找到”错误!!
最佳答案
响应“404 Not Found”清楚地表明您已成功向服务器发出 HTTP 请求,服务器返回了此 HTTP 响应。所以问题似乎并不是由本地主机部分引起的。
例如,您可能缺少一些必需的 header 。
比较浏览器发出的完整 HTTP 请求和 HttpClient 发出的完整 HTTP 请求。
关于java - HttpClient 无法与 localhost 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16193322/