对于HttpGet类型,方法addHeader(String,String)是未定义的

时间:2015-08-14 18:19:55

标签: java rest apache-httpclient-4.x

我有这个程序:

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;

public class ApplicationRESTFul {

    public static void main(String[] args) {

        String url = "http://www.google.com/search?q=httpClient";

        HttpClient client = HttpClientBuilder.create().build();
        HttpGet request = new HttpGet(url);

        request.addHeader("Accept", "application/json");

    }

}

但是我从eclipse得到了这条消息

the method addHeader (String, String) is undefined for the type HttpGet

我正在使用这个库,正如我在文档中看到的那样,该方法应该存在(org.apache.httpcomponents.httpclient_4.5)

http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/methods/HttpGet.html

enter image description here

1 个答案:

答案 0 :(得分:3)

我通过将httpcore JAR添加到类路径来解决它。从maven添加依赖项也会添加httpcore JAR,而不仅仅是httpclient JAR,这也是它的原因。