ClientWithResponseHandler示例给出了错误?

时间:2011-11-03 11:38:10

标签: httpclient execute http-get

我努力解决这个问题但我做不到。我正在尝试从apache使用httpClient 4.1.2。作为逻辑,我从示例开始,问题是我有一些我不理解的奇怪错误。这是交易:

    package ClientWithResponseHandler;
    import org.apache.http.client.ResponseHandler;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.BasicResponseHandler;
    import org.apache.http.impl.client.DefaultHttpClient;

public class Main {

public final static void main(String[] args) throws Exception {

    HttpClient httpclient = new DefaultHttpClient();
    try {
        HttpGet httpget = new HttpGet("http://www.google.com/");

        System.out.println("executing request " + httpget.getURI());

        // Create a response handler
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseBody = **httpclient.execute(httpget, responseHandler);**
        System.out.println("----------------------------------------");
        System.out.println(responseBody);
        System.out.println("----------------------------------------");

    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }
}

}

错误是“httpclient.execute(httpget,responseHandler);” IT说它找不到方法execute(HttpGet,ResponseHandler) 这个例子不应该起作用吗?我究竟做错了什么?! :S

1 个答案:

答案 0 :(得分:0)

我也得到了同样的错误。我通过添加“httpcore-4.2.1.jar”来解决它。然后它开始抱怨没有发现日志记录的类Def。所以我添加了“commons-logging-1.1.1.jar”,现在我认为它工作正常。这两个文件都可以与“httpclient-4.2.1.jar”一起找到。

希望这有帮助。