简体   繁体   English

如何使用Jsoup登录页面

[英]How to log in to pages using Jsoup

After a couple hours of searching, I'm still a bit stumped as to how to access an html page after I log in. Looking at the various other posts on here as well as the Jsoup API, I understand that accessing the page after the log-in page will require some code like this: 经过几个小时的搜索,我仍然对登录后如何访问html页面感到有些困惑。查看此处的其他各种帖子以及Jsoup API,我了解到登录页面将需要以下代码:

    Connection.Response loginForm = Jsoup.connect("https://parentviewer.pisd.edu/")
            .method(Connection.Method.GET)
            .execute();

    Document document = Jsoup.connect("https://parentviewer.pisd.edu/")
            .data("username", "testUser")
            .data("password", "testPass")
            .data("LoginButton", "Login")
            .cookies(loginForm.cookies())
            .post();

However, I think my understanding may be a little skewed, as I still don't quite undestand exactly what I should put for each value. 但是,我认为我的理解可能有点歪斜,因为我仍然不太完全理解应该为每个值输入什么。

For example, on the website of , would I be using input name="ctl00$ContentPlaceHolder1$portalLogin$UserName" as the key and "testUser" as the value? 例如,在的网站上,我是否将输入名称=“ ctl00 $ ContentPlaceHolder1 $ portalLogin $ UserName”作为键并将“ testUser”作为值?

Is my method of approaching this task even correct? 我处理此任务的方法是否正确? Any help is greatly appreciated. 任何帮助是极大的赞赏。

Yes, this code will look like yours. 是的,此代码看起来像您的代码。

Connection.Response loginForm = Jsoup.connect("https://parentviewer.pisd.edu/")
        .method(Connection.Method.GET)
        .execute();

Document document = Jsoup.connect("https://parentviewer.pisd.edu/")
        .data("ctl00$ContentPlaceHolder1$portalLogin$UserName", "testUser")
        .data("ctl00$ContentPlaceHolder1$portalLogin$Password", "testPass")
        .cookies(loginForm.cookies())
        .post();

System.out.println(document.body().html());

How to make this working? 如何使它工作? Best way is to enable Web Developer Console in your browser and login this page. 最好的方法是在浏览器中启用Web Developer Console并登录此页面。 After this check what is sended from broswer to server and send this data with JSoup. 完成此检查后,将从浏览器发送到服务器的内容发送给JSoup,并使用JSoup发送此数据。

In your example request data look like this: 在您的示例中,请求数据如下所示:

Request URL:https://parentviewer.pisd.edu/
Request Method:POST
Status Code:200 OK

FormData:
__LASTFOCUS:
__EVENTTARGET:
__EVENTARGUMENT:
__VIEWSTATE:/wEPDwULLTEwNjY5NzA4NTBkZMM/uYdqyffE27bFnREF10B/RqD4
__SCROLLPOSITIONX:0
__SCROLLPOSITIONY:106
__EVENTVALIDATION:/wEdAASCW34hepkNwIXSnvGxEUTlqcZt0XO7QUOibAd3ocrpayqHxD2e5zCnWBj9+m7TCi0S+C76MEjhL0ie/PsBbOp+Shjkt2W533uAqvBQcWZNXoh672M=
ctl00$ContentPlaceHolder1$portalLogin$UserName:testUser@gmail.com
ctl00$ContentPlaceHolder1$portalLogin$Password:testPass
ctl00$ContentPlaceHolder1$portalLogin$LoginButton:Login

Not all data are required, try with minimal request and check if this works. 并非所有数据都是必需的,请以最少的请求尝试并检查是否可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM