当前位置: 首页 > 知识库问答 >
问题:

无法使用带有x-www-form-urlencoded参数的JSOUP登录网站

公良鸿禧
2023-03-14

我怎样才能实现下面的请求通过使用J汤?

POST/login/user HTTP/1.1
主机:url。出版报纸。co.il
缓存控制:无缓存内容类型:应用程序/x-www-form-urlencoded

用户名=只读

我尝试了以下代码,但它不工作,我得到一个错误的网站

未收到预期的安全令牌

我正在使用以下代码:

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

我知道,当我使用x-www-form-urlencoded时,我需要在URL中对其进行编码,但假设当我设置正确的头JSOUP时,我会这样做,我错了吗?

非常感谢。

共有1个答案

淳于哲
2023-03-14

您应该传递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());

 类似资料:
  • 问题内容: REST API映射到Java对象时,采用输入内容类型: 在表单输入请求中,我正在设置my_name和my_phone的值,但是MyRequest对象带有myName和myPhone作为空值。 我正在使用Jackson批注2.3 jar 有什么建议可能有什么问题吗? 问题答案: 我最近在使用SpringMVC和Jackson时遇到了同样的问题! 在Spring中,当您将端点显式配置为仅

  • 我在这里使用了这个很棒的资源--https://forums.aws.amazon.com/thread.jspa?messageid=673012--但是如果数据中有空字段(我会这样做,这超出了我的控制),它会抛出一个内部服务器错误。 然后从Christian E Willman-https://github.com/christianewillman/aws-api-gateway-bodyP

  • 我正在尝试使用,但数据没有到达PHP。我认为这是因为当使用标题始终设置为

  • 我正在尝试使用jsoup登录一个网站,我很确定我正在解析所有需要解析的东西,我只是不知道出了什么问题。 我用这个做参考:http://cs.harding.edu/fmccown/android/Logging-into-Pipeline.pdf 以下是我的AsycntTask doInBackground中的代码: 但问题是,当我登录时,它不包含页面的文档,它包含一个错误页面的文档,该页面只显示

  • 我有一个Jersey 2.22 rest web服务,我的一个endpoint有以下签名: 我使用PostMan调用这个web服务,在post请求中不传递任何特定的头,我将“query”form param传递为“x-www-form-urlencoded”。 jersey应用程序由Tomcat 7托管,该Tomcat 7以JVM编码UTF-8文件启动。编码UTF-8,并将连接器的URIEncod

  • 在本规范公布的时候,“application/x-www-form-urlencoded”媒体类型在[W3C.REC-html401-19991224]的17.13.4节中定义但未在IANA MIME媒体类型注册表([http://www.iana.org/assignments/media-types](http://www.iana.org/assignments/media-types))中