开发者

HTTPRequest class not recognised, doesnt appear to have an apache.jar?

开发者 https://www.devze.com 2023-03-12 08:00 出处:网络
I\'m trying to compile the following code (to see if i can get a web serverto stream to a client): HttpClient httpClient = new HttpClient();

I'm trying to compile the following code (to see if i can get a web serverto stream to a client):

           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();
           }

but eclipse doesn't recognise the HTTPRequest class and i dont have any apache jars which support it, even though i added all the apache .jars to my build path?


That's the HttpComponents Client 4.x API. You can download it here. Perhaps you had version 3.x instead which indeed lacks the HttpRequest class.


I know it is a late answer but I came across this problem myself.

How I encountered the Problem

In Eclipse I used maven and the HttpRequest class by just binding the httpclient of org.apache.httpcomponents likes this:

for the dependency:

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

for copying by the maven compiler plugin as a package into the target/dependency folder:

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

After doing that I could easily use the HttpRequest class within the eclipse environment.

Nevertheless, after executing mvn package and starting the created jar of my application, I got a ClassNotFoundException for org.apache.http.HttpRequest.

The Solution that worked for me

So if you ever come across this problem, make sure you include other jars that are required by the httpclient for proper functioning (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>

Please refer to the mvn repositories for the most recent versions.

0

精彩评论

暂无评论...
验证码 换一张
取 消