【问题标题】:the method addHeader (String, String) is undefined for the type HttpGetHttpGet 类型的方法 addHeader (String, String) 未定义
【发布时间】:2015-08-14 18:19:55
【问题描述】:

我有这个程序:

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

【问题讨论】:

  • 对我来说很好......
  • 您还有其他导入类名为HttpGet的类吗?
  • 试试这个:org.apache.http.client.methods.HttpGet request = new org.apache.http.client.methods.HttpGet(url);
  • 我遇到了同样的错误 :-(

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    从 maven 导入依赖而不是在类路径中添加 lib 解决了问题

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-10
      • 1970-01-01
      • 2015-07-30
      • 1970-01-01
      • 1970-01-01
      • 2012-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多