2

How to login via jsoup?

  <table border="0" cellpadding="8px"> 
   <tbody>
    <tr> 
     <td align="left"> <span id="ctl00_bodyContent_LabelTurni1" style="font-size: 13pt;">Nome utente</span> </td> 
     <td align="left"> <input name="ctl00$bodyContent$txtUser" type="text" size="30" id="ctl00_bodyContent_txtUser"> </td> 
    </tr> 
    <tr> 
     <td align="left"> <span id="ctl00_bodyContent_LabelTurni2" style="font-size: 13pt;">Password</span> </td> 
     <td align="left"> <input name="ctl00$bodyContent$txtPassword" type="password" size="30" id="ctl00_bodyContent_txtPassword" onfocus="this.select();"> </td> 
    </tr> 
    <tr> 
     <td> </td> 
     <td align="center"> <input type="submit" name="ctl00$bodyContent$btnLogin" value="Conferma" onclick="ValidateConfirm(); return false;WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$bodyContent$btnLogin&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" id="ctl00_bodyContent_btnLogin"> <input type="submit" name="ctl00$bodyContent$btnEsci" value="Esci" onclick="window.close(); return false;" id="ctl00_bodyContent_btnEsci"> </td> 
    </tr> 
   </tbody>
  </table> 

I tried this, but it does not work:

Document doc = (Document) Jsoup.connect("http://turni.contacts.com/Default.aspx").data("ctl00_bodyContent_txtUser", "user").data("ctl00_bodyContent_txtPassword", "password").data("ctl00_bodyContent_btnLogin","Conferma")
//.cookies(res.cookies()).timeout(0).post();
ashatte
  • 5,442
  • 8
  • 39
  • 50
Enzo
  • 597
  • 1
  • 8
  • 22

1 Answers1

1

Usually login into a web site requires two steps -

  1. You send a GET request to get the page, and you extract from there some values like session ID etc, and the cookies.
  2. You send a POST request with the values from step 1, and your user name and password.

To know which values you need to send, use your browser in the developer mode (by pressing F12) and examine the traffic. Change the user agent string to match your browser, since some sites send different pages to different clients. You can see an example here.

Community
  • 1
  • 1
TDG
  • 5,909
  • 3
  • 30
  • 51
  • It's hard to tell without accessing the actual page. I've tried http://turni.contacts.com/Default.aspx but couldn't reach it. Is it the real address? – TDG Jan 04 '16 at 20:00
  • Try to add ALL the fields that are present at the POST request, even the empty ones - __LASTFOCUS, __EVENTTARGET... – TDG Jan 04 '16 at 20:22