【问题标题】:Jsoup Cookies for HTTPS scraping用于 HTTPS 抓取的 Jsoup Cookie
【发布时间】:2011-10-31 15:16:56
【问题描述】:

我正在尝试使用此站点在欢迎页面上收集我的用户名来学习 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) 总是以 NULL 结尾。如果我在网络浏览器中删除此 cookie,我将失去连接。所以我确信它是正确的cookie。另外,如果我更改代码

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

我可以从这个页面上抓取我的登录名。这也让我觉得我有正确的 cookie。那么,我的 cookie 怎么会出现 Null 呢?我的用户名和密码名称字段是否不正确?还有什么?

谢谢。

【问题讨论】:

    标签: java cookies web-scraping jsoup


    【解决方案1】:

    我知道我在这里迟到了 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 证书。您必须以我还没有完全弄清楚的方式正确管理它们。

    【讨论】:

      【解决方案2】:

      我总是分两步来做这件事(就像正常人一样),

      1. 读取登录页面(通过 GET,读取 cookie)
      2. 提交表单和 cookie(通过 POST,无 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从previuos请求设置到下一个使用

               .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();
          }
      }
      

      【讨论】:

        【解决方案3】:

        如果您尝试获取和传递所有 cookie 而不假设这样的情况会怎样:Sending POST request with username and password and save session cookie

        如果您仍有问题,请尝试查看:Issues with passing cookies to GET request (after POST)

        【讨论】:

        • 我尝试了第一个链接并且能够取回三个 cookie,但一个是空白的。我需要的 cookie 不存在,这就解释了为什么我总是得到 NULL。我无法弄清楚为什么我的代码没有返回我在 firebug 中看到的所有 cookie。有什么可以找的吗?
        猜你喜欢
        • 2012-09-05
        • 2014-11-23
        • 2021-07-24
        • 2013-01-26
        • 2013-01-02
        • 1970-01-01
        • 1970-01-01
        • 2019-04-14
        • 2013-06-01
        相关资源
        最近更新 更多