Jsoup HTTPS抓取网页时如何处理Cookies

31

我正在尝试使用这个网站,在欢迎页面上收集我的用户名,以学习Jsoup和Android。 使用以下代码:

Connection.Response res = Jsoup.connect("http://www.mikeportnoy.com/forum/login.aspx")
    .data("ctl00$ContentPlaceHolder1$ctl00$Login1$UserName", "username", "ctl00$ContentPlaceHolder1$ctl00$Login1$Password", "password")
    .method(Method.POST)
    .execute();
String sessionId = res.cookie(".ASPXAUTH");

Document doc2 = Jsoup.connect("http://www.mikeportnoy.com/forum/default.aspx")
.cookie(".ASPXAUTH", sessionId)
.get();

我的cookie (.ASPXAUTH) 总是为空。如果我在web浏览器中删除这个cookie,我就会失去连接,所以我确定它是正确的cookie。此外,如果我更改代码

.cookie(".ASPXAUTH", "jkaldfjjfasldjf")  Using the correct values of course

我能从这个页面上解析出我的登录名。这也让我认为我的 cookie 是正确的。那么,为什么我的 cookie 会变成 Null?是我的用户名和密码字段写错了吗?还是其他原因?

谢谢。

3个回答

53

我知道我有点晚了,已经晚了10个月。但是使用Jsoup的一个很好的选项是使用这个简单易懂的代码:

//This will get you the response.
Response res = Jsoup
    .connect("url")
    .data("loginField", "login@login.com", "passField", "pass1234")
    .method(Method.POST)
    .execute();

//This will get you cookies
Map<String, String> cookies = res.cookies();

//And this is the easieste way I've found to remain in session
Documente doc = Jsoup.connect("url").cookies(cookies).get();

虽然我仍然有连接到某些网站的问题,但是我使用同样的基本代码连接了很多网站。哦,还有,我忘了提醒一下..我发现我的问题就在于SSL证书。你必须以一种我仍然没有完全弄清楚的方式正确管理它们。


这个解决方案对我没有用,请您能否澄清一下 https://stackoverflow.com/questions/73572751/jsoup-login-cookies-are-not-working-with-sub-pages-but-only-with-home-page - vikramvi

19

我通常会分两步来完成这个操作(就像普通人一样):

  1. 通过 GET 请求读取登录页面(读取 cookies)
  2. 通过 POST 请求提交表单和 cookies(不进行 cookie 操作)

例如:

Connection.Response response = Jsoup.connect("http://www.mikeportnoy.com/forum/login.aspx")
        .method(Connection.Method.GET)
        .execute();

response = Jsoup.connect("http://www.mikeportnoy.com/forum/login.aspx")
        .data("ctl00$ContentPlaceHolder1$ctl00$Login1$UserName", "username")
        .data("ctl00$ContentPlaceHolder1$ctl00$Login1$Password", "password")
        .cookies(response.cookies())
        .method(Connection.Method.POST)
        .execute();

Document homePage = Jsoup.connect("http://www.mikeportnoy.com/forum/default.aspx")
        .cookies(response.cookies())
        .get();

始终使用前一个请求中的cookie设置下一个请求,方法如下:

         .cookies(response.cookies())

SSL在这里并不重要。如果您遇到证书问题,可以执行此方法来忽略SSL。

public static void trustEveryone() {
    try {
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });

        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new X509TrustManager[]{new X509TrustManager() {
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { }

            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { }

            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        }}, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
    } catch (Exception e) { // should never happen
        e.printStackTrace();
    }
}

这个解决方案对我不起作用,你能否请澄清一下 https://stackoverflow.com/questions/73572751/jsoup-login-cookies-are-not-working-with-sub-pages-but-only-with-home-page - vikramvi
我按照完全相同的步骤操作,但是它没有起作用,你能否请看一下上面的查询? - vikramvi
如果您在证书方面遇到问题,该如何知道?另外,请澄清一下,在第三步中使用cookie时,如何调试为什么无法登录? - vikramvi

1

1
我尝试了第一个链接,成功获取了三个cookie,但其中一个是空的。我需要的cookie不在那里,这就解释了为什么我总是得到NULL。我无法弄清楚为什么我的代码没有返回firebug中看到的所有cookie。有什么我可以寻找的吗? - Brian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接