Skip to content

Jsoup post form data example

Jsoup post form data example shows how to post form data to a website using Jsoup. The example also shows how to post form data by inspecting the HTML source.

How to post form data using Jsoup?

First, make sure to set proper user agent, referrer, and connection timeouts for the Jsoup connection. Jsoup supports the HTTP POST method. You can use method method of Connection class to post the form data using Jsoup.

This method sets the request method to GET or POST. The default method used by the Jsoup is GET.

First of all, you need to determine below given two things to post data using Jsoup.

1) Form action URL, where the data needs to be posted.
2) All request parameters including hidden parameters. For the HTML form, it means names and values of all the input, select, textarea, etc.

Locate the HTML form which needs to be posted. Let’s take an example HTML form like given below.

The above-given form has 5 parameters, out of that 3 inputs are hidden. We need to send all 5 values including hidden to the form action URL “/postpage”. Since the action URL is a relative path, we need to make it absolute using the base URL of the website we want to post data to.

Once all the parameters have been determined, we can connect to the action URL and send post parameters using the data method of Connection class.

This method sets the request parameter. If the method is GET, the parameter is sent as a query string. If the method is POST, the parameter is posted in the request body.

Example

Instead of using data method for individual post parameters, you can also use data method which accepts a map containing all the post parameter key-value pairs.

Please also visit how to set Jsoup proxy and Jsoup referer examples to know more.

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. hi there,
    I tried logging in with the code above but always got the html attributes of the error page while logging in. Can you please suggest me a way to find what the error is.

    I am able to log in with same username and password on the site.
    *******************************
    /*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */
    package jsonfetcher;

    /**
    *
    * @author Divyalaptus
    */
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;

    import org.jsoup.Jsoup;
    import org.jsoup.Connection.Response;
    import org.jsoup.nodes.Document;
    public class JsonFetcher {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here

    try {

    //grab login form page first
    Response loginPageResponse =
    Jsoup.connect(“https://www.icloudemserp.com/corecampus/checkuser1.php”)
    .referrer(“https://www.icloudemserp.com/corecampus/index.php”)
    .userAgent(“Mozilla/5.0”)
    .timeout(10 * 1000)
    .followRedirects(true)
    .execute();

    System.out.println(“Fetched login page”);

    //get the cookies from the response, which we will post to the action URL
    Map mapLoginPageCookies = loginPageResponse.cookies();
    String YOUR_USER_ID = “MREI2033”;
    String YOUR_PASSWORD = “student1234”;
    String branch_id = “9”;
    //lets make data map containing all the parameters and its values found in the form
    Map mapParams = new HashMap();
    mapParams.put(“userid”, “MREI2033”);
    mapParams.put(“pass_word”, “student1234”);
    mapParams.put(“branchid”, “9”);
    mapParams.put(“Submit”, “Submit”);

    System.out.println(“*****************************************”+YOUR_USER_ID+YOUR_PASSWORD);
    //URL found in form’s action attribute
    String strActionURL = “https://www.icloudemserp.com/corecampus/checkuser1.php”;

    Response responsePostLogin = Jsoup.connect(strActionURL)
    //referrer will be the login page’s URL
    .referrer(“https://www.icloudemserp.com/corecampus/index.php”)
    //user agent
    .userAgent(“Mozilla/5.0”)
    //connect and read time out
    .timeout(10 * 1000)
    //post parameters
    .data(mapParams)
    //cookies received from login page
    .cookies(mapLoginPageCookies)
    //many websites redirects the user after login, so follow them
    .followRedirects(true)
    .execute();

    System.out.println(“HTTP Status Code: ” + responsePostLogin.statusCode());

    //parse the document from response
    Document document = responsePostLogin.parse();
    System.out.println(document);

    //get the cookies
    Map mapLoggedInCookies = responsePostLogin.cookies();

    /*
    * For all the subsequent requests, you need to send
    * the mapLoggedInCookies containing cookies
    */

    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    }

    *******************************
    plzz help, in need.

    Thanks
    Divyansh Kumar

    1. Are you missing any parameters while posting? Use Chrome inspect to check the login form and see if you are missing any hidden or normal parameters that needs to be passed.

Leave a Reply

Your email address will not be published.