Java 如何将“HttpClient”导入 Eclipse?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4290419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to import "HttpClient" to Eclipse?
提问by alex
How do I import "HttpClient" in Eclipse? I downloaded HttpClient from http://hc.apache.org/downloads.cgijust now. I added it to my Eclipse new java project and want to run a example copy from website.
如何在 Eclipse 中导入“HttpClient”?我刚刚从http://hc.apache.org/downloads.cgi下载了 HttpClient 。我将它添加到我的 Eclipse 新 java 项目中,并希望从网站运行示例副本。
This example uses import org.apache.commons.httpclient.*;
But, what pity is, it shows that Eclipse can't resolve this.
这个例子使用了import org.apache.commons.httpclient.*;
但是,可惜的是,它表明Eclipse无法解决这个问题。
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?
现在,我想知道将新发布的 HttpClient 导入我的项目的正确方法。是否有必要在类路径中添加一些 jar?它是什么?
This is the whole example I run. I guess new released "HTTPClient" changed its import jar, is it true?
这是我运行的整个示例。我猜新发布的“HTTPClient”改变了它的导入 jar,是真的吗?
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;
}
}
采纳答案by Thorbj?rn Ravn Andersen
You drag the jar file to your project so you can see it inside Eclipse.
您将 jar 文件拖到您的项目中,以便您可以在 Eclipse 中看到它。
To give it special meaning to Eclipse, right click on the jar file inside Eclipse and select Build Path -> Add to Build Path.
为了赋予 Eclipse 特殊的意义,右键单击 Eclipse 中的 jar 文件并选择 Build Path -> Add to Build Path。
Now your imports should resolve properly.
现在您的导入应该正确解析。
回答by Kusuma
It works, it Solved:
它有效,它解决了:
- first download file JAR from web apache https://hc.apache.org/downloads.cgi.
- extract file zip
- open your eclipse project
- Do right click libs on the Package Explorer and choose Build Path -> Configure Build Path
- Choose Java Build Path on the left side box
- Click tab Libraries.
- Add External JAR, choose your extracted file on point (2)
- You may choose all file JAR on extracted file, it depends on your imported at your project.
- 首先从 web apache https://hc.apache.org/downloads.cgi下载文件 JAR 。
- 解压文件 zip
- 打开你的日食项目
- 在 Package Explorer 上右键单击 libs 并选择 Build Path -> Configure Build Path
- 在左侧框中选择 Java Build Path
- 单击选项卡库。
- 添加外部 JAR,在点 (2) 上选择您提取的文件
- 您可以选择提取文件中的所有文件 JAR,这取决于您在项目中导入的内容。
回答by Bar Horing
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.....
下载 *****.tar.gz 文件
提取它
进入 lib 文件夹,在那里你会找到所有的 JAR
打开 Eclipse,右键单击您的项目 -> 属性 -> Java 构建路径 -> 库选项卡 -> 添加外部 JAR -> 选择 lib 中的所有 JAR(步骤 4)
为了进行测试,我建议您尝试运行一些使用此库的代码,例如:http: //www.mkyong.com/java/apache-httpclient-examples/
您可能会看到一个红色下划线,将其悬停并选择导入.....
good luck
祝你好运