Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login a website with JSoup post method

Tags:

html

post

jsoup

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!

like image 357
Amir Avatar asked Nov 24 '13 05:11

Amir


People also ask

What is the use of Jsoup in Java?

What It Is. jsoup can parse HTML files, input streams, URLs, or even strings. It eases data extraction from HTML by offering Document Object Model (DOM) traversal methods and CSS and jQuery-like selectors. jsoup can manipulate the content: the HTML element itself, its attributes, or its text.


1 Answers

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.

like image 73
Alkis Kalogeris Avatar answered Oct 16 '22 16:10

Alkis Kalogeris