Java 无法使用带有x-www-form-urlencoded参数的JSOUP登录网站
如何使用实现以下请求 POST/login/user HTTP/1.1Java 无法使用带有x-www-form-urlencoded参数的JSOUP登录网站,java,jsoup,Java,Jsoup,如何使用实现以下请求 POST/login/user HTTP/1.1 主持人:url.publishedprices.co.il 缓存控制:没有缓存 内容类型:application/x-www-form-urlencoded username=readonly&password=123456&csrftoken=wohewqfDrcK2JMK5w7BKw4jCuMOiARnDg01Rw4VZdQ%3D%3D 我尝试了下面的代码,但它不起作用,我从一个 未收到预期的安全令牌 我正在使用以下代码
主持人:url.publishedprices.co.il
缓存控制:没有缓存 内容类型:application/x-www-form-urlencoded username=readonly&password=123456&csrftoken=wohewqfDrcK2JMK5w7BKw4jCuMOiARnDg01Rw4VZdQ%3D%3D 我尝试了下面的代码,但它不起作用,我从一个 未收到预期的安全令牌 我正在使用以下代码:
Document welcomePage = Jsoup.connect("https://url.publishedprices.co.il/login").get();
Element inputHidden = welcomePage.getElementById("csrftoken");
String securityTokenKey = inputHidden.attr("name");
String securityTokenValue = inputHidden.attr("value");
Connection.Response res2 = Jsoup.connect("https://url.publishedprices.co.il/login/user")
.header("Content-Type","application/x-www-form-urlencoded;charset=UTF-8")
.data("username", "readonly")
.data("password", "123456")
.data(securityTokenKey, securityTokenValue)
.method(Method.POST)
.execute();
System.out.println(res2.body());
Map<String, String> loginCookies = res2.cookies();
Document welcomePage=Jsoup.connect(“https://url.publishedprices.co.il/login).get();
元素inputHidden=welcomePage.getElementById(“csrftoken”);
字符串securityTokenKey=inputHidden.attr(“名称”);
字符串securityTokenValue=inputHidden.attr(“值”);
Connection.Response res2=Jsoup.connect(“https://url.publishedprices.co.il/login/user")
.header(“内容类型”,“应用程序/x-www-form-urlencoded;字符集=UTF-8”)
.数据(“用户名”、“只读”)
.数据(“密码”、“123456”)
.数据(securityTokenKey、securityTokenValue)
.method(method.POST)
.execute();
System.out.println(res2.body());
Map loginCookies=res2.cookies();
我知道,当我使用x-www-form-urlencoded
时,我需要在URL中对其进行编码,但假设当我设置正确的头JSOUP时,我会这样做,我错了吗
谢谢。您应该传递cookie(其中包含带有秘密令牌的会话),以便服务器端的CSRF保护能够比较令牌并授予您访问权限
Connection.Response res1 = Jsoup.connect("https://url.publishedprices.co.il/login").method(Method.GET).execute();
Document welcomePage = res1.parse();
Map welcomCookies = res1.cookies();
Element inputHidden = welcomePage.getElementById("csrftoken");
String securityTokenKey = inputHidden.attr("name");
String securityTokenValue = inputHidden.attr("value");
Connection.Response res2 = Jsoup.connect("https://url.publishedprices.co.il/login/user")
.header("Content-Type","application/x-www-form-urlencoded;charset=UTF-8")
.data("username", "readonly")
.data("password", "123456")
.data(securityTokenKey, securityTokenValue)
.cookies(welcomCookies)
.method(Method.POST)
.execute();
System.out.println(res2.body());