Login with Jsoup Java

若如初见. 提交于 2020-01-10 05:36:08

问题


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();

回答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.



来源:https://stackoverflow.com/questions/34551819/login-with-jsoup-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!