首页 > 解决方案 > Jsoup 和 cookie - 我无法登录到所有子站点

问题描述

我有关于在 Jsoup 中保存 cookie 的问题。我通过 JSON 登录主站点,这里一切正常。当我想转到下一个子站点时遇到问题,我放置了相同的 cookie,但这是行不通的。在我的控制台中,我可以看到我在登录之前有 html 站点。任何想法我应该改变什么?

public class MyBenefit {
    final static String USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Safari/605.1.15";
    final static String LOGIN_FORM_URL = "https://system.mybenefit.pl/mybenefit/login.html";
    final static String LOGIN_ACTION_URL = "https://system.mybenefit.pl/mybenefit/login/by_email";
    final static String URL = "https://system.mybenefit.pl/mybenefit/benefit-login-dispatcher.html";
    final static String URLQUERY = "https://system.mybenefit.pl/mybenefit/global-search.html?query=";
    final static String USERNAME = "xxx";
    final static String PASSWORD = "xxx";

    public static void main(String[] args) {

        try {
            Connection.Response loginForm = Jsoup.connect(LOGIN_FORM_URL)
                    .method(Connection.Method.GET)
                    .userAgent(USER_AGENT)
                    .execute();

            //here I create json, because json is required to login
            JsonBuilderFactory jsonBuilderFactory = Json.createBuilderFactory(Collections.EMPTY_MAP);
            JsonObject jsonObject = jsonBuilderFactory.createObjectBuilder()
                    .add("password", PASSWORD)
                    .add("captcha", "")
                    .add("lang", "pl")
                    .add("email", USERNAME).build();


            HashMap<String, String> cookies = new HashMap<>(loginForm.cookies());

            Connection.Response homePage = Jsoup.connect(LOGIN_ACTION_URL)
                    .cookies(cookies)
                    .ignoreContentType(true)
                    .requestBody(jsonObject.toString())
                    .method(Connection.Method.POST)
                    .userAgent(USER_AGENT)
                    .execute();

            //here I get cookie after logg
            String sessionID = homePage.cookie("BENEFIT_SESSIONID");

            //this connection is working
            Connection.Response afterLogg = Jsoup.connect(URL)
                    .cookie("BENEFIT_SESSIONID", sessionID)
                    .execute();

            Document document = afterLogg.parse();

            //this connection is NOT workingg good, I am not logged on website
            Connection.Response test = Jsoup.connect(URLQUERY)
                    .cookie("BENEFIT_SESSIONID", sessionID)
                    .execute();

            System.out.println(test.parse());


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

我在 Firefox 中查看了开发人员工具,页面 URL 和页面 URLQUERY 具有相同的 cookie。我发了一些关于这个的图片。登录URL 后的 cookie URLQUERY上的 cookie

标签: cookiesjsoup

解决方案


推荐阅读