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;
}
}
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.
It works, it Solved:
Go to: https://hc.apache.org/downloads.cgi
download the *****.tar.gz file
extract it
go inside the lib folder, there you'll find all the JARs
open Eclipse, right click on your project -> Properties -> Java Build Path -> Libraries tab -> Add External JARs - > choose all the JARs in lib (step 4)
to test I recommend you try to run some code that uses this library like: http://www.mkyong.com/java/apache-httpclient-examples/
you'll probably see a red underline, hover it and choose Import.....
good luck
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With