Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import "HttpClient" to Eclipse?

Tags:

java

How do I import "HttpClient" in Eclipse? I downloaded HttpClient from http://hc.apache.org/downloads.cgi just now. I added it to my Eclipse new java project and want to run a example copy from website.

This example uses import org.apache.commons.httpclient.*; But, what pity is, it shows that Eclipse can't resolve this.

Now, I want to know the right way to import new released HttpClient to my project. Is it necessary to add some jar to classpath? What is it?

This is the whole example I run. I guess new released "HTTPClient" changed its import jar, is it true?

package http.demo; 
import java.io.IOException; 
import org.apache.commons.httpclient.*; 
import org.apache.commons.httpclient.methods.*;

public class SimpleHttpClient { 
   public static void main(String[] args) throws IOException {
      HttpClient client = new HttpClient();
      client.getHostConfiguration().setHost( "www.imobile.com.cn" , 80, "http" );
      method = getPostMethod();    
      client.executeMethod(method);  
      System.out.println(method.getStatusLine());   
      Stringresponse=newString(method.getResponseBodyAsString().getBytes("8859_1"));

      System.out.println(response);
      method.releaseConnection();
   }


   private static HttpMethod getGetMethod(){
      return new GetMethod("/simcard.php?simcard=1330227");
   }

    private static HttpMethod getPostMethod(){
      PostMethod post = new PostMethod( "/simcard.php" );
      NameValuePair simcard = new NameValuePair( "simcard" , "1330227" );
      post.setRequestBody( new NameValuePair[] { simcard});
      return post; 
   } 
}
like image 544
alex Avatar asked Nov 27 '10 06:11

alex


3 Answers

You drag the jar file to your project so you can see it inside Eclipse.

To give it special meaning to Eclipse, right click on the jar file inside Eclipse and select Build Path -> Add to Build Path.

Now your imports should resolve properly.

like image 32
Thorbjørn Ravn Andersen Avatar answered Oct 22 '22 22:10

Thorbjørn Ravn Andersen


It works, it Solved:

  1. first download file JAR from web apache https://hc.apache.org/downloads.cgi.
  2. extract file zip
  3. open your eclipse project
  4. Do right click libs on the Package Explorer and choose Build Path -> Configure Build Path
  5. Choose Java Build Path on the left side box
  6. Click tab Libraries.
  7. Add External JAR, choose your extracted file on point (2)
  8. You may choose all file JAR on extracted file, it depends on your imported at your project.
like image 84
Kusuma Avatar answered Oct 22 '22 23:10

Kusuma


  1. Go to: https://hc.apache.org/downloads.cgi

  2. download the *****.tar.gz file

  3. extract it

  4. go inside the lib folder, there you'll find all the JARs

  5. open Eclipse, right click on your project -> Properties -> Java Build Path -> Libraries tab -> Add External JARs - > choose all the JARs in lib (step 4)

  6. to test I recommend you try to run some code that uses this library like: http://www.mkyong.com/java/apache-httpclient-examples/

  7. you'll probably see a red underline, hover it and choose Import.....

good luck

like image 4
Bar Horing Amir Avatar answered Oct 23 '22 00:10

Bar Horing Amir