问题
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("ctl00$bodyContent$btnLogin", "", true, "", "", 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 -
- You send a
GETrequest to get the page, and you extract from there some values like session ID etc, and the cookies. - You send a
POSTrequest 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