当我登录网站时,jsoup不会返回所有cookie

时间:2015-07-09 09:58:00

标签: java cookies jsoup

我正在尝试登录网站,但我的代码只返回2个Cookie而不是10个以上的Cookie。这是我正在使用的:

cookies = Jsoup.connect("https://www.e-cigarette-forum.com/forum/login")
                .userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36")
                .data("login", "username")
                .data("register", "0")
                .data("password", "password")
                .data("submit", "Log in")
                .data("remember", "1")
                .data("cookie_check", "1")
                .data("_xfToken", "")
                .data("redirect", "https://www.e-cigarette-forum.com/forum/")
                .followRedirects(false)
                .method(org.jsoup.Connection.Method.POST).timeout(50000).execute().cookies();

当我打印cookies时,我得到了以下cookie:

  

xf_session = c683e3cd72e296ec0bc8d4e36403fc1a

     

__ cfduid = d82ccf7651e7a7cf07c102a7782476c081436435336

有谁知道我错过了什么?

1 个答案:

答案 0 :(得分:1)

相反:

  

https://www.e-cigarette-forum.com/forum/login

您必须使用:

  

https://www.e-cigarette-forum.com/forum/login/login

enter image description here

工作代码:

try {

            String url = "https://www.e-cigarette-forum.com/forum/login/login/";

            Connection.Response response = Jsoup.connect(url)
                    .userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36")
                    .data("login", "username")
                    .data("password", "password")
                    .method(Connection.Method.POST)
                    .followRedirects(true)
                    .execute();

            Document document = response.parse();

            System.out.println("Hi " + document.select(".username.NoOverlay").html());

            Document doc = Jsoup.connect("https://www.e-cigarette-forum.com/forum/account/personal-details")
                    .userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36")
                    .cookies(response.cookies())
                    .followRedirects(true)
                    .get();

            System.out.println(doc);

        } catch (IOException e) {
            e.printStackTrace();
        }