HttpClient不能与localhost一起使用

时间:2013-04-24 13:29:17

标签: java web-services localhost apache-commons-httpclient

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

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

1 个答案:

答案 0 :(得分:-1)

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

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

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