使用Jsoup Java登录 [英] Login with Jsoup Java

查看:43
本文介绍了使用Jsoup Java登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过jsoup登录?

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

推荐答案

通常,登录网站需要两个步骤-

Usually login into a web site requires two steps -

  1. 您发送GET请求以获取页面,然后从中提取内容 一些值,例如会话ID等,以及cookie.
  2. 您发送一个POST请求,其中包含步骤1中的值以及您的用户名和密码.
  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.

要知道需要发送哪些值,请在开发人员模式下使用浏览器(按F12键)并检查流量.更改user agent字符串以匹配您的浏览器,因为某些站点将不同的页面发送到不同的客户端.您可以在此处看到示例.

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.

这篇关于使用Jsoup Java登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆