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.
1 |
public Connection method(Connection.Method method) |
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.
1 2 3 4 5 6 7 8 9 |
<form name="frmdemo" id="frmdemo" method="post" action="/postpage"> <span>Login Id:</span> <input type="text" name="txtloginid" id="txtloginid" value=""> <span>Password:</span> <input type="text" name="txtloginpassword" id="txtloginpassword" value=""> <input type="hidden" name="random" value="123342343"> <input type="hidden" name="task" value="login"> <input type="hidden" name="destination" value="/welcome"> </form> |
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.
1 |
Connection data(String key, String value) |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
package com.javacodeexamples.libraries.jsoup; import java.io.IOException; import java.util.Map; import org.jsoup.Connection.Method; import org.jsoup.Connection.Response; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; public class JsoupPostDataExample { public static void main(String[] args) { try{ Response response = Jsoup.connect("http://www.example.com/postpage") .userAgent("Mozilla/5.0") .timeout(10 * 1000) .method(Method.POST) .data("txtloginid", "YOUR_LOGINID") .data("txtloginpassword", "YOUR_PASSWORD") .data("random", "123342343") .data("task", "login") .data("destination", "/welcome") .followRedirects(true) .execute(); //parse the document from response Document document = response.parse(); //get cookies Map<String, String> mapCookies = response.cookies(); /* * You may need to send all the cookies you received * from the post response to the subsequent requests. * * You can do that using cookies method of Connection */ }catch(IOException ioe){ System.out.println("Exception: " + ioe); } } } |
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.
1 |
public Connection data(Map<String, String> postData) |
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.
How would one get a useful read a useful response from document? just start with jsoup Thanks in Advance. Tony
Sorry, I did not get your question. Do you mean how to read webpage and extract data using jsoup?
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.
Hi Divyansh kuma
I have similar problem with click button of site.
I want to know, What was the problem on your situation?
Awesome, Thanks so much
Desde Republica Dominicana.
I am glad you liked it. Thanks.
Hii
i want to post the data using jsoup library through urls. plzz help me anyone urgent.
Hello Prasad,
That is exactly what this example explains. Are you stuck somewhere?
Thanks.
i appreciate this sir, thank you very much…… but how can i download music using jsoup