Login a website with JSoup post method

Amir picture Amir · Nov 24, 2013 · Viewed 9.8k times · Source

I'm trying to login into a website with JSoup post method. I saw some examples but neither are working to me. I'm trying to login to: http://ug.technion.ac.il/Tadpis.html For that i have the following code:

 String url = "http://ug.technion.ac.il/Tadpis.html";
 doc = Jsoup.connect(url).data("userid", "my_user_id")
                .data("password", "my_password").data("function","signon").data("submit", "Signon").post();

Apparently I'm missing some data (I don't know which). Another thing that isn't clear enought to me is the url. When examinig the html of the above url i can see this line:

 <form action="http://techmvs.technion.ac.il:80/cics/wmn/wmngrad?aapmlkwi&ORD=1&s=1" method="POST" name="SignonForm"

which is a different url from the one stated above. Which one of these do i suppose to use as the url parameter to "connect" method?

Thanks!

Answer

Alkis Kalogeris picture Alkis Kalogeris · Dec 1, 2013

The url that you see in the address bar is not the one that you want to make the request to. You should make the request to the second url that you see in the form.

//With this you login and a session is created
    Connection.Response res = Jsoup.connect("http://techmvs.technion.ac.il:80/cics/wmn/wmngrad?aapmlkwi&ORD=1&s=1")
        .data("username", "myUsername", "password", "myPassword")
        .method(Method.POST)
        .execute();

//This will get you cookies
Map<String, String> loginCookies = res.cookies();

//Here you parse the page that you want. Put the url that you see when you have logged in
Document doc = Jsoup.connect("urlYouNeedToBeLoggedInToAccess")
      .cookies(loginCookies)
      .get();

P.S. I believe that http://techmvs.technion.ac.il:80/cics/wmn/wmngrad is enough. You don't need the extra GET parameters, but check it for yourself.