如何将"HttpClient"导入Eclipse?

ale*_*lex 12 java

如何在Eclipse中导入"HttpClient"?我刚刚从http://hc.apache.org/downloads.cgi下载了HttpClient .我将它添加到我的Eclipse新java项目中,并希望从网站运行示例副本.

这个例子使用import org.apache.commons.httpclient.*;但是,遗憾的是,它表明Eclipse无法解决这个问题.

现在,我想知道将新发布的HttpClient导入我的项目的正确方法.是否有必要在classpath中添加一些jar?它是什么?

这是我运行的整个例子.我想新发布的"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; 
   } 
}
Run Code Online (Sandbox Code Playgroud)

小智 11

它有效,它解决了:

  1. 首先从web apache https://hc.apache.org/downloads.cgi下载文件JAR .
  2. 提取文件zip
  3. 打开你的日食项目
  4. 在Package Explorer上右键单击libs,然后选择Build Path - > Configure Build Path
  5. 在左侧框中选择Java Build Path
  6. 单击选项卡库.
  7. 添加外部JAR,在点(2)上选择提取的文件
  8. 您可以在解压缩的文件中选择所有文件JAR,这取决于您在项目中导入的内容.


Tho*_*sen 8

您将jar文件拖到项目中,以便在Eclipse中查看它.

要赋予Eclipse特殊的意义,请右键单击Eclipse中的jar文件,然后选择Build Path - > Add to Build Path.

现在您的导入应该正确解决.