如何使用Jsoup登录页面 [英] How to log in to pages using Jsoup

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

问题描述

经过几个小时的搜索,登录后如何访问html页面仍然让我有些困惑.查看此处的其他各种帖子以及Jsoup API,我了解访问登录页面后的页面将需要如下代码:

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:

    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.

例如,在的网站上,我将输入名称="ctl00 $ ContentPlaceHolder1 $ portalLogin $ UserName"作为键,将"testUser"作为值吗?

For example, on the website of , would I be using input name="ctl00$ContentPlaceHolder1$portalLogin$UserName" as the key and "testUser" as the value?

我完成这项任务的方法是否正确? 任何帮助,我们将不胜感激.

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

如何使其正常工作?最好的方法是在浏览器中启用 Web开发人员控制台并登录此页面.完成此检查后,从浏览器发送到服务器的内容将发送给Jsoup,并通过JSoup发送.

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.

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

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