3

我在 java Jersey REST 中开发了一个 Web 服务。我使用 HttpClient 来调用一个基本的 GET 方法。

在浏览器上使用此 URLhttp://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),它给出了 Not Found 错误!

4

1 回答 1

0

响应“404 Not Found”清楚地表明您成功地向服务器发出了 HTTP 请求,服务器返回了这个 HTTP 响应。所以问题似乎并不源于 localhost 部分。

例如,您可能缺少一些必需的标头。

比较浏览器发出的完整 HTTP 请求和 HttpClient 发出的完整 HTTP 请求。

于 2013-04-24T13:35:37.187 回答