5

嗨,我不断收到错误消息 在此处输入图像描述

test.java:15: package org.apache.commons.httpclient does not exist
import org.apache.commons.httpclient.Cookie;
                                ^
test.java:16: package org.apache.commons.httpclient does not exist
import org.apache.commons.httpclient.HttpState;
                                ^
test.java:17: package org.apache.commons.httpclient does not exist
import org.apache.commons.httpclient.HttpClient;
                                ^
test.java:18: package org.apache.commons.httpclient.methods does not exist
import org.apache.commons.httpclient.methods.GetMethod;
                                        ^
test.java:22: cannot find symbol
symbol  : class HttpClient
location: class test
            HttpClient client = new HttpClient();
            ^
test.java:22: cannot find symbol
symbol  : class HttpClient
location: class test
            HttpClient client = new HttpClient();
                                    ^
test.java:26: cannot find symbol
symbol  : class GetMethod
location: class test
            GetMethod method = new GetMethod("https://online.investools.com/authentication/auth.iedu");
            ^
test.java:26: cannot find symbol
symbol  : class GetMethod
location: class test
            GetMethod method = new GetMethod("https://online.investools.com/authentication/auth.iedu");
                                   ^
test.java:29: cannot find symbol
symbol  : class Cookie
location: class test
                              Cookie[] cookies = client.getState().getCookies();
                              ^
test.java:31: cannot find symbol
symbol  : class Cookie
location: class test
                                    Cookie cookie = cookies[i];
                                    ^
10 errors

编译我用

javac -cp ;./httpclient-4.2.jar;jsoup-1.6.3.jar test.java

这是代码

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.apache.commons.httpclient.Cookie;  
import org.apache.commons.httpclient.HttpState;  
import org.apache.commons.httpclient.HttpClient;  
import org.apache.commons.httpclient.methods.GetMethod; 


public class test{
public static void main (String []args)throws IOException{
    HttpClient client = new HttpClient();  
    client.getParams().setParameter("username", "SomeUSER");  
    client.getParams().setParameter("password", "GF@QT#$WE");  

    GetMethod method = new GetMethod("https://online.investools.com/authentication/auth.iedu");  
        try{  
              client.executeMethod(method);  
              Cookie[] cookies = client.getState().getCookies();  
              for (int i = 0; i < cookies.length; i++) {  
                Cookie cookie = cookies[i];  
                System.err.println(  
                  "Cookie: " + cookie.getName() +  
                  ", Value: " + cookie.getValue() +  
                  ", IsPersistent?: " + cookie.isPersistent() +  
                  ", Expiry Date: " + cookie.getExpiryDate() +  
                  ", Comment: " + cookie.getComment());  
                }  
              client.executeMethod(method);  
        } 
        catch(Exception e) {  
          System.err.println(e);  
        } 
        finally {  
          method.releaseConnection();  
        }

我很困惑,不知道我做错了什么。我认为这很简单,但我检查了几次,它存在并且 jsoup 编译得很好。谢谢

4

3 回答 3

8

org.apache.http.client如果您使用的是HttpClient 4.2(看起来像您),我相信您会想要。该org.apache.commons.httpclient软件包适用于旧版本

编辑:不是所有的课程httpclient现在都在http.client;有些只是在http。此外,还需要进行其他更改 - 例如,HttpClient现在是一个接口,因此您不能像那样实例化它。基本上,您已经获得了 3.x 代码,因此您应该将其更新为 4.x 或使用 3.x jar 文件。

于 2012-06-11T19:59:53.747 回答
1

我从这里找到了我丢失的版本:http: //mvnrepository.com/artifact/commons-httpclient/commons-httpclient

dependencies {
    compile 'commons-httpclient:commons-httpclient:3.1'
}
于 2016-05-01T13:25:09.733 回答
0

使用 jar -xvf httpclient-4.2.java temp

检查 org/apache/commons/httpclient/Cookie 是否存在,如果不存在则错误/损坏的 jar

也尝试 javac -cp .;./httpclient-4.2.jar;jsoup-1.6.3.jar test.java 而不是 javac -cp ;./httpclient-4.2.jar;jsoup-1.6.3.jar test.java

于 2012-06-11T20:06:11.050 回答