【问题标题】:jsoup does not send cookies from previous requests - bug?jsoup 不会从以前的请求中发送 cookie - 错误?
【发布时间】:2016-04-06 06:26:23
【问题描述】:

我正在对我的银行帐户进行一些网络抓取。 所有请求都发往同一个域。 我以这样的方式开始: res = Jsoup.connect().cookies(res.cookies()) 除了第一个请求。 cookie 应该被重用,一些是在请求之间添加的。 有一些 POST 和 GET 请求,用户代理和一些标头已设置。

我收到错误 401,这意味着凭据问题 - Fiddler 显示 Jsoup 没有在最后一个请求中发送 cookie。没有迹象表明服务器要求删除一些 cookie,而且网站在浏览器中运行良好,所以我认为问题出在我这边。

令人惊讶的是,当我保存要映射的 cookie 并将它们附加到此请求时,一切正常。我不能公开提供确切的数据,因为它是我的银行账户,但我可以为开发者提供 cookie/捕获的网络数据包。

这是一个错误吗?这是我的代码:

import java.io.IOException;
import java.util.Map;

import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;



public class Test {

/**
 * @param args
 * @throws IOException 
 * @throws UnirestException 
 */
public static void main(String[] args) throws IOException {


    String userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1";


    //get login page
    Response res = Jsoup
        .connect("https://example.com/")
        .userAgent(userAgent)
        .execute();




    //send login
    res = Jsoup
        .connect("https://example.com/login")
        .userAgent(userAgent)
        .cookies(res.cookies())
        .data("redirect", "/")
        .data("login", "1234")
        .method(Method.POST)
        .execute();

    //System.out.print(res.body());



    //send password
    res = Jsoup
        .connect("https://example.com/login")
        .userAgent(userAgent)
        .cookies(res.cookies())
        .data("redirect", "/")
        .data("user", "1234")
        .data("password", "1234")
        .method(Method.POST)
        .execute();

    //System.out.print(res.body());







    Map<String, String> cookies = res.cookies();

    //json
    //here cookies are sent properly
    res = Jsoup
        .connect("https://example.com/0/0/list.json?d=1451669517333")
        .userAgent(userAgent)
        .cookies(res.cookies())
        .method(Method.GET)
        .ignoreContentType(true)
        .execute();

    System.out.print(res.body());


    //json      
    //here is the problem with cookies - fix is to use Map of cookies from above
    res = Jsoup
        .connect("https://example.com/ord/0/0?a=23000&d=1451669539678")
        .userAgent(userAgent)
        .cookies(cookies)
        .header("Host", "example.com")
        .header("Connection", "keep-alive")
        .header("Accept", "application/json, text/plain, */*")
        .header("X-Requested-With", "XMLHttpRequest")
        .header("Referer", "https://example.com/")
        .header("Accept-Encoding", "gzip, deflate, lzma, sdch")
        .header("Accept-Language", "pl,en-US;q=0.8,en;q=0.6,de;q=0.4")
        .method(Method.GET)
        .ignoreContentType(true)
        .execute();

    System.out.print(res.body());

}

}

【问题讨论】:

  • 你使用什么版本的 Jsoup?
  • 我认为很多.header() 是不必要的。尝试删除 Accept-Encoding,我不确定 Jsoup 是否支持所有这些。
  • 您是否检查了哪些 cookie 与第二个但最后一个请求一起发回?也许在这个请求中,您银行的网络服务器没有响应所有以前的 cookie?你的解决方法对我来说似乎是合法的,所以我建议使用这种机制。
  • @TDG:最新版本 1.8.3
  • @JonasCz:我在调试过程中删除了所有标题,cookie 仍然丢失

标签: cookies https jsoup screen-scraping


【解决方案1】:

由于第二个但也是最后一个答案似乎没有返回任何 cookie,因此您不能将该响应用作最终查询的 cookie 源。 JSoup 不会自动为您处理 cookie。在每个请求中,您需要指定要发送的 cookie - 正如您所做的那样。但是您也每次都用新的响应覆盖变量res。如果您不将连接的 cookie 保存在地图中,则旧的 cookie 将与响应一起被删除。所以你使用地图的方法是完全有效的,我会继续使用这种模式。

如果您想要更自动的 cookie 管理,我建议您使用 Apache httpClient 库。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-20
    • 2015-02-11
    • 1970-01-01
    • 2013-06-08
    • 2020-04-28
    • 1970-01-01
    • 2019-02-11
    相关资源
    最近更新 更多