简体   繁体   中英

Need help using Jsoup to automate login on web page

I want to automate the login on a web page using Java. I have set user id using Jsoup but don't know how to send that data to browser.

You can login to a web page using jsoup. The code will look like as:

Connection.Response loginForm = Jsoup.connect("https://login.to/")
                .ignoreContentType(true)
                .userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0")
                .referrer("http://www.google.com")
                .timeout(12000)
                .followRedirects(true)
                .method(Connection.Method.GET)
                .execute();

Connection.Response loginFormFilled = Jsoup.connect("https://login.to/")
            .ignoreContentType(true)
            .userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0")
            .followRedirects(true)
            .referrer("https://login.to/")
            .data("LOGON", "user")//check the form to find field name for user name
            .data("PASSWORDS", "pass")//check the form to find field name for user password
            .cookies(loginForm.cookies())
            .method(Connection.Method.POST)
            .execute();
            int statusCode = loginFormFilled.statusCode();
            Map<String, String> cookies = loginFormFilled.cookies();

You should check what is sent to a server while login in order to populate data for second request. After this check if the response code is 200, which means success. You can also check cookies and there should be JSESSIONID or something similar indicating that you have a session, which is common way for interacting with loged in users.

According to their home page :

jsoup is a Java library for working with real-world HTML. It provides a very convenient API for extracting and manipulating data, using the best of DOM, CSS, and jquery-like methods.

So I doubt that you can use it submit data to the browser.

That being said, you might want to take a look at Selenium (it has its own Java Client ).

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