如何使用jsoup发布表单登录?

我想在这里登录 在此处输入图像描述 源代码

:: Dhaka Electric Supply Company Limited (DESCO)::       img{ border:0px; }          
function checkLogin() { if( document.login.username.value == '') { alert( 'Please enter your account number' ); return false; }return true; } alert('Payments through VISA and Master Card are stopped by DBBL. only DBBL Nexus card is allowed.');
Account No.
Help
Before, use this facility/services please read instructions...

Back Home


ABOUT SSL CERTIFICATES
Total Visits: 1

基本上下面的代码就是表格

 
Account No.
Help

在此处输入图像描述

我正在尝试这个

 package jsouptest; import org.jsoup.Connection; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; public class JsouptTest { public static void main(String[] args) throws Exception { Connection.Response loginForm = Jsoup.connect("https://www.desco.org.bd/ebill/login.php") .method(Connection.Method.GET) .execute(); Document document = Jsoup.connect("https://www.desco.org.bd/ebill/login.php") .data("cookieexists", "false") .data("username", "32007702") .data("login", "Login") .cookies(loginForm.cookies()) .post(); System.out.println(document); } } 

但我得到低于错误

 Exception in thread "main" java.net.SocketTimeoutException: Read timed out at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.read(SocketInputStream.java:152) at java.net.SocketInputStream.read(SocketInputStream.java:122) at sun.security.ssl.InputRecord.readFully(InputRecord.java:442) at sun.security.ssl.InputRecord.read(InputRecord.java:480) at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:927) at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323) at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563) at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185) at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153) at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:439) at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:424) at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:178) at jsouptest.JsouptTest.main(JsouptTest.java:12) 

我缺少什么? 怎么解决?

您为执行POST请求而使用的URL是错误的,因为当您必须对表单执行特定请求时,您应该使用表单标记中存在的网页,在本例中为“authentication.php” ”。

所以代码将是:

  package jsouptest; import org.jsoup.Connection; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; public class JsouptTest { public static void main(String[] args) throws Exception { Connection.Response loginForm = Jsoup.connect("https://www.desco.org.bd/ebill/login.php") .method(Connection.Method.GET) .execute(); Document document = Jsoup.connect("https://www.desco.org.bd/ebill/authentication.php") .data("cookieexists", "false") .data("username", "32007702") .data("login", "Login") .cookies(loginForm.cookies()) .post(); System.out.println(document); } } 

这个正确地检索您想要的网页。

对我来说问题是我没有发送viewstate,eventvalidation,viewstategenerator值。

要获得这些值,您必须先向登录表单页面发送GET请求。 在我的实例中,这是一个default.aspx页面。

然后你必须提取这些值并将它们放入变量中。 当然,您还需要表单登录和密码字段ID,提交按钮的ID等。 有关发送的所有变量的列表,请手动登录并使用chrome dev工具(检查元素)查看POST请求的网络选项卡。 在里面它应该显示您提交的用户名和密码。 在那里,您将看到发送的其他变量。

dev工具示例中的网络选项卡

接下来,发送包含所有这些变量的POST请求,将响应中的cookie保存到变量中,然后您可以使用它来转到另一个页面。

码:

 import java.io.IOException; import java.util.Map; import org.jsoup.Connection; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.nodes.FormElement; public class get_example { public static void main(String[] args) throws IOException { //=================================== Connection.Response response2 = Jsoup.connect("yourloginpage") .method(Connection.Method.GET) .execute(); Document responseDocument = response2.parse(); Element eventValidation = responseDocument.select("input[name=__EVENTVALIDATION]").first(); Element viewState = responseDocument.select("input[name=__VIEWSTATE]").first(); Element viewStateGen = responseDocument.select("input[name=__VIEWSTATEGENERATOR]").first(); Connection.Response response = Jsoup.connect("yourloginpage") .method(Connection.Method.POST) .data("ctl00$plnMain$txtLogin", "username") .data("ctl00$plnMain$txtPassword", "password") .data("ctl00$plnMain$Submit1", "Log In") .data("ctl00$ucCopyright$hdnPrivacyStatement", "Privacy Statement") .data("ctl00$ucCopyright$hdnTermsOfUse", "Terms of Use") .data("__VIEWSTATE", viewState.attr("value")) .data("__VIEWSTATEGENERATOR", viewStateGen.attr("value")) .data("__EVENTVALIDATION", eventValidation.attr("value")) .execute(); Map cookies = response.cookies(); Document homePage = Jsoup.connect("anotherpage") .cookies(cookies) .get(); System.out.println(homePage.body().html()); } } 

就我而言,它包括三个步骤:

  1. 从登录表单页面获取cookie;
  2. 将凭据发布到登录表单action= URL,更新cookie;
  3. 用户将被重定向到索引页面。 不要在这里解析,只需将Response发送到最终目标URL。
  4. 到达最后一页时解析。

Cookie应该在每个步骤中更新,有时会添加条目,有时您必须清除它并重置。 请务必使用Internet Explorer / Chrome / Firefox Dev工具( F12 )在网络面板中检查网络传输, cookies部分。