cookies - 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
解决方案
推荐阅读
- facebook - facebook graph api - 获取评论 facebook 页面帖子的用户详细信息
- java - 扩展阔叶默认管理控制器
- javascript - Webpack 入口点语法混乱
- angular - Angular 将字符串发布到 asp.net 后端
- java - XSLT concat 计数器与字段名称,使其动态
- floating-point - 位数除以指数和尾数
- airflow - 尝试从本地 Airflow 运行 DataProcSparkOperator 任务时出现 HttpError 400
- command - 在 nightwatch.js 中调用自定义命令
- javascript - 如何在 Web 模拟 XAMPP 中使用 AJAX
- safari - 如何使用 workbox-webpack-plugin 在 Safari 中缓存 .mp4 文件?