简体   繁体   中英

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:

    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?

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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