Cookies jsoup不从以前的请求发送cookie-bug?

Cookies jsoup不从以前的请求发送cookie-bug?,cookies,https,jsoup,screen-scraping,Cookies,Https,Jsoup,Screen Scraping,我正在对我的银行账户进行一点网络抓取。 所有请求都指向同一个域。 我是这样开始的: res=Jsoup.connect()。Cookie应该被重用,一些是在请求之间添加的。 有一些POST和GET请求,设置了用户代理和一些头 我得到了错误401,这意味着凭证问题-Fiddler已经表明Jsoup在最后一个请求中没有发送cookie。并没有迹象表明服务器要求删除一些cookie,而且该网站在浏览器中运行良好,所以我认为问题出在我这边 令人惊讶的是,当我将cookies保存到map并将它们附加到这个

我正在对我的银行账户进行一点网络抓取。 所有请求都指向同一个域。 我是这样开始的:
res=Jsoup.connect()。Cookie应该被重用,一些是在请求之间添加的。
有一些POST和GET请求,设置了用户代理和一些头

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

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

是虫子吗?这是我的密码:

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

}

}
import java.io.IOException;
导入java.util.Map;
导入org.jsoup.Connection.Method;
导入org.jsoup.Connection.Response;
导入org.jsoup.jsoup;
公开课考试{
/**
*@param args
*@抛出异常
*@unirest异常
*/
公共静态void main(字符串[]args)引发IOException{
字符串userAgent=“Mozilla/5.0(Windows NT 6.1;WOW64;rv:40.0)Gecko/20100101 Firefox/40.1”;
//获取登录页面
响应res=Jsoup
.连接(“https://example.com/")
.userAgent(userAgent)
.execute();
//发送登录
res=Jsoup
.连接(“https://example.com/login")
.userAgent(userAgent)
.cookies(res.cookies())
.数据(“重定向”、“/”)
.数据(“登录”、“1234”)
.method(method.POST)
.execute();
//System.out.print(res.body());
//发送密码
res=Jsoup
.连接(“https://example.com/login")
.userAgent(userAgent)
.cookies(res.cookies())
.数据(“重定向”、“/”)
.数据(“用户”、“1234”)
.数据(“密码”、“1234”)
.method(method.POST)
.execute();
//System.out.print(res.body());
映射cookies=res.cookies();
//json
//在这里,饼干被正确地发送
res=Jsoup
.连接(“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
//这里是Cookie的问题-修复是使用上面的Cookie映射
res=Jsoup
.连接(“https://example.com/ord/0/0?a=23000&d=1451669539678")
.userAgent(userAgent)
.饼干(饼干)
.header(“主机”、“example.com”)
.header(“连接”、“保持活动”)
.header(“接受”、“应用程序/json、文本/普通、*/*”)
.header(“X-request-With”,“XMLHttpRequest”)
.header(“Referer”https://example.com/")
.header(“接受编码”、“gzip、deflate、lzma、sdch”)
.标题(“接受语言”,“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());
}
}

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


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

您使用的是什么版本的Jsoup?我认为很多
.header()
都是不必要的。尝试删除Accept编码,我不确定Jsoup是否支持所有这些。您是否检查了第二个但最后一个请求返回的cookie?可能在该请求中,您银行的Web服务器没有响应之前的所有cookie?您的解决方法对我来说似乎是合法的,所以我建议使用这种机制。@TDG:1.8的最新版本。3@JonasCz:我在调试期间删除了所有标题,cookies仍然丢失