External Libraries

Jsoup post form data example

Jsoup post form data example shows how to post form data to a website using Jsoup. 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 Jsoup. Jsoup supports HTTP POST method. You can use method method of Connection class to post the form data using Jsoup.

This method sets request method to GET or POST. Default method used by 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 all the input, select, textarea etc. field’s name and values.

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

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 action URL is a relative path, we need to make it absolute using 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 data method of Connection class.

This method sets the request parameter. If the method is GET, parameter is sent as query string. If method is POST, parameter is posted in 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.

Visit Jsoup examples for more. Please let us know your views in the comments section below.

Want to learn quickly?
Try one of the many quizzes. I promise you will not be disappointed.

Tags

About the author

rahimv

rahimv

rahimv has over 15 years of experience in designing and developing Java applications. His areas of expertise are J2EE and eCommerce. If you like the website, follow him on Facebook, Twitter or Google Plus.

4 Comments

  • 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

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

Your email address will not be published. Required fields are marked *