简体   繁体   中英

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

I have this program:

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");

    }

}

But I got this message from eclipse

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

I am using this library and as I see in the documentation , the method should exist (org.apache.httpcomponents.httpclient_4.5)

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

在此处输入图片说明

I solved it by adding httpcore JAR to class path. Adding the dependency from maven adds the httpcore JAR too and not just the httpclient JAR, and that's why it works too.

importing the depency from maven instead to add the lib in the classpath solved the problem

 <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.6</version>
        </dependency>  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM