JSoup POST身份验证

时间:2013-04-10 08:29:22

标签: java authentication post login jsoup

我正在尝试使用基本用户名和网站连接到网站密码验证,保留cookie并解析需要登录页面上提供的cookie的站点的数据。当从本地Apache Web服务器使用网站副本时,解析工作非常完美。这是认证给我带来麻烦。我正在使用Eclipse作为我的IDE,并且我继续使用以下代码收到错误:

    Response res = Jsoup
        .connect("site_with_login")
        .data("login", "MyUsername")
        .data("pass", "MyPassword")
        .method(Method.POST)
        .execute();

    //Keep logged in
    Map<String, String> cookies = res.cookies();

.method(Method.POST)是问题所在,因为错误发生在POST。这是运行解析时的输出:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
POST cannot be resolved or is not a field

at parseHTML.main(parseHTML.java:26)

这很奇怪,因为stackoverflow上的很多帖子都提到了认证方式。我导入了所有打包的文件并在我的库中安装了jsoup-1.7.2.jar文件。

有谁知道如何解决这个烦人的问题?

提前致谢。

编辑: 我目前正在使用以下软件包:

    import org.jsoup.Connection.Method;
    import org.jsoup.Connection;
    import org.jsoup.Jsoup;
    import org.jsoup.helper.HttpConnection.Response;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;

我现在发布的整个代码块都会出现以下错误:

    Type mismatch: cannot convert from Connection.Response to HttpConnection.Response

The whole code is available here

1 个答案:

答案 0 :(得分:2)

我认为您使用了错误的导入。

确保使用:

.method(Connection.Method.POST)

确保导入: org.jsoup.Connection.Method而非java.lang.reflect.Method

另一个导入问题:

import org.jsoup.Connection.Response;

而不是:

import org.jsoup.helper.HttpConnection.Response;