HTTPRequest类无法识别,似乎没有apache.jar?

时间:2011-06-11 00:31:11

标签: java eclipse apache

我正在尝试编译以下代码(看看我是否可以获得客户端的web serverto流):

           HttpClient httpClient = new HttpClient();

           HttpRequest req = new HttpRequest("GET", "http://tools.ietf.org/html/rfc2616.html");

           // returns immediately if the complete header (not message!) is received
           HttpResponse resp = httpClient.call(req);

           if (resp.getStatus() == 200) {  
              // create the output file 
              File file = new File("rfc2616.html");
              file.createNewFile();
              FileChannel fc = new RandomAccessFile(file, "rw").getChannel();

              // get a blocking message body channel
              ReadableByteChannel inputBodyChannel = resp.getBlockingBody();

              // and transfer the data
              fc.transferFrom(inputBodyChannel, 0, 900000);
              fc.close();
           }

但是eclipse不识别HTTPRequest类,我没有任何支持它的apache jar,即使我将所有的apache .jars添加到我的构建路径中?

2 个答案:

答案 0 :(得分:0)

那是HttpComponents Client 4.x API。您可以下载here。也许你有版本3.x而不是HttpRequest类。

答案 1 :(得分:0)

我知道这是一个迟到的答案,但我自己也遇到了这个问题。

我是如何遇到问题的

在Eclipse中,我使用maven和HttpRequest类只需绑定 org.apache.httpcomponents 的httpclient就像这样:

表示依赖:

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

用于将maven编译器插件作为包复制到目标/依赖项文件夹中:

<artifactItem>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.5</version>
  <type>jar</type>
  <overWrite>true</overWrite>
</artifactItem>

在这样做之后,我可以在eclipse环境中轻松使用HttpRequest类。

然而,在执行mvn package并启动我的应用程序的创建jar之后,我得到了org.apache.http.HttpRequest的ClassNotFoundException。

适用于我的解决方案

因此,如果您遇到此问题,请确保包含httpclient所需的其他正常功能的jar( httpcore commons-logging commons-codec ):

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpcore</artifactId>
  <version>4.4.1</version>
</dependency>

<dependency>
  <groupId>commons-logging</groupId>
  <artifactId>commons-logging</artifactId>
  <version>1.2</version>
</dependency>

<dependency>
  <groupId>commons-codec</groupId>
  <artifactId>commons-codec</artifactId>
  <version>1.10</version>
</dependency>

有关最新版本,请参阅mvn repositories

相关问题