Java HTML / XML How to - Post form login using jsoup








Question

We would like to know how to post form login using jsoup.

Answer

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
/*w ww.  j av  a  2 s  .c om*/
public class Main {
  public static void main(String[] args) throws Exception {

    Connection.Response loginForm = Jsoup
        .connect("https://www.your server.com/login.php")
        .method(Connection.Method.GET).execute();

    Document document = Jsoup
        .connect("https://www.your server.com/authentication.php")
        .data("cookieexists", "false").data("username", "YourName")
        .data("login", "Login").cookies(loginForm.cookies()).post();
    System.out.println(document);

  }

}