3

经过几个小时的搜索,我仍然对登录后如何访问 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();

但是,我认为我的理解可能有点偏差,因为我仍然不太了解我应该为每个值添加什么。

例如,在 的网站上,我是否会使用 input name="ctl00$ContentPlaceHolder1$portalLogin$UserName" 作为键和 "testUser" 作为值?

我完成这项任务的方法是否正确?任何帮助是极大的赞赏。

4

1 回答 1

6

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. After this check what is sended from broswer to server and send this data with 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.

于 2014-01-12T13:27:00.873 回答