Skip to content

Jsoup login to website using post method example

Jsoup login to a website using post method example shows how to login to a website by posting all parameters using Jsoup. The example also shows how to manage cookies while logging in to the website.

How to login to the website using Jsoup using the POST method?

Step 1:

Have a look at how to post form data using Jsoup example.

Step 2:

Open the website login page in your browser which supports inspecting the HTML elements to make things easier. For this example, we are going to login to http://www.rediff.com.

We need to inspect the login form so that we can determine
1) All required form parameters.
2) Where login form data needs to be submitted.

Here is the login form.

_jsoup_login1

We need to determine the action URL and form data fields. Right click on the login Id text field and select inspect menu item as given in the screenshot.

_jsoup_login2

It will open the developer tools with elements tab opened in the lower half of the browser. Tools will also have a login id input element selected in the source. Now, locate the parent form element of the login id input element.

_jsoup_login3

This is the form we are interested in. URL mentioned in the “action” attribute of the form is where we need to post the data. We also need to send all inputs (including hidden), textarea and select elements contained in the form along with name and values as parameters in the POST method. Make a note of all the parameter names and values.

Once the parameters and action URL is determined, we can go ahead and log in to the website using Jsoup as given below.

Output

Disclaimer: The URLs given/accessed in the example are purely for demonstration purposes. You should not access any URLs in any way which is not permitted by the website’s terms of service agreement.

Please make sure to set proper user agent and referrer before making a connection, most of the web servers reject requests not having proper user agent string and referrer values. If you are behind a proxy server, you may also need to set Jsoup proxy.

If you are unable to determine the action URL and/or parameters, you can use the developer devtools to determine that. First open the login page in a chrome browser, press F12 to open the devtools, and go to the Network tab.

Click the Clear button to clear older requests. Now enter the user id and password and click the login button. The network tab will display the outgoing requests. Click on the relevant raw in the devtools to see the parameters and other details.

This example is a part of the Jsoup tutorial with examples.

Please let me know your views in the comments section below.

About the author

10 comments

    1. Hello Shirisha,

      Unfortunately I cannot. You will have to do trial and error to make it work.

      One more thing, If you have to fill the captcha before submitting any form, this method does not work. Captcha is designed to prevent such type of automatic submissions by programs.

      Best of luck!

  1. Hi Rahimv,
    I followed your rediff mail code example to login a website to fetch web contents after successful login.
    I am getting HTTP status code as 200, but I am not getting the web contents after successful login.
    May I know the reason for this issue or any clues which will help me to fix this issue?

    1. Hi Rajakumar,

      There could be many reasons. Is the website redirecting you to another page after login is successful?. I would suggest you to study the complete flow of events and pages using the browser first and then only implement it using Jsoup.

      If the login page is returning you the HTTP code 200, then in the same session try to open a webpage that only authenticated users can view (you need to forward the cookies you received).

      If that does not work, I suspect you are missing something in the flow. Trial and error is the way to go in that case.

      Thank you.

Leave a Reply

Your email address will not be published.