Jsoup使用表单登录(发布) [英] Jsoup login with form (post)

查看:101
本文介绍了Jsoup使用表单登录(发布)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读后,一些

After reading some examples, I wanted to implement a crawler for helpshift with login such as:

https://target.helpshift.com/login/? next =%2Fadmin%2Fissues%2F

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class JsouptTest {

    public static void main(String[] args) throws Exception {
        int x = 1;
        Connection.Response loginForm = Jsoup.connect("https://target.helpshift.com/login/?next=%2Fadmin%2Fissues%2F" + x + "%2F")
                .method(Connection.Method.GET)
                .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0")
                .execute();

        Document document = Jsoup.connect("https://target.helpshift.com/login/")
                .data("cookieexists", "false")
                .data("username", "email@example.com")
                .data("password", "123456")
                .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0")
                .cookies(loginForm.cookies())
                .post();
        System.out.println(document);

    }

}

但是,我遇到此错误:

线程"main" org.jsoup.HttpStatusException中的异常:HTTP错误 提取网址.状态= 403,URL = https://target.helpshift.com/login/ org.jsoup.helper.HttpConnection $ Response.execute(HttpConnection.java:537) 在 org.jsoup.helper.HttpConnection $ Response.execute(HttpConnection.java:493) 在org.jsoup.helper.HttpConnection.execute(HttpConnection.java:205) 在org.jsoup.helper.HttpConnection.post(HttpConnection.java:200)处 edu.utfpr.helpcrawler.JsouptTest.main(JsouptTest.java:32)

Exception in thread "main" org.jsoup.HttpStatusException: HTTP error fetching URL. Status=403, URL=https://target.helpshift.com/login/ at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:537) at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:493) at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:205) at org.jsoup.helper.HttpConnection.post(HttpConnection.java:200) at edu.utfpr.helpcrawler.JsouptTest.main(JsouptTest.java:32)

推荐答案

如果您检查请求标头,您将看到它在完成时发送cookie,但是它也在表单数据中包含了cookie的一部分. .将此添加到您的第二个请求

If you check the request headers you will see the it sends the cookies as you've done, but it includes a part of the cookie in the form data too. Add this to your second request

.data("_csrf_token", loginForm.cookie("_csrf_token"))

这篇关于Jsoup使用表单登录(发布)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆