Java 如何使用jsoup发布表单登录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23320498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to post form login using jsoup?
提问by MD TAHMID HOSSAIN
i want to login in heresource code
我想在这里登录源代码
<HTML><HEAD><TITLE>:: Dhaka Electric Supply Company Limited (DESCO)::</TITLE>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<META http-equiv=Pragma content=no-cache>
<META http-equiv=imagetoolbar content=no>
<META content="Home Page of Dhaka Electric Supply Company Limited" name=description>
<META content="MSHTML 6.00.2900.2180" name=GENERATOR>
<style type="text/css">
img{
border:0px;
}
</style>
<script type="text/javascript" src="js/ajax-dynamic-content.js"></script>
<script type="text/javascript" src="js/ajax.js"></script>
<script type="text/javascript" src="js/ajax-tooltip.js"></script>
<link rel="stylesheet" href="css/ajax-tooltip.css" media="screen" type="text/css">
<link rel="stylesheet" href="css/ajax-tooltip-demo.css" media="screen" type="text/css">
<LINK media=all href="css/global.css" type=text/css rel=stylesheet>
</HEAD>
<BODY>
<DIV id=over style="DISPLAY: none"></DIV>
<DIV class=baselayout>
<DIV class=pagetopshadow></DIV>
<DIV class=basebg>
<DIV class=pageleft></DIV>
<DIV class=pagecenter>
<DIV id=Header>
</DIV>
<DIV id=Menu>
</DIV>
<DIV id=HomeContent>
<DIV class=right>
<DIV class=homeintro>
<div style="padding-top: 0px;">
<script>
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.');
</script>
<form NAME='login' METHOD='POST' ACTION='authentication.php'>
<table width="350" height="181"cellpadding='0' cellspacing='0' border='0' style="border:#e5e5e5 0px solid; BACKGROUND: url(css/images/top9.png) no-repeat left top;" align="center">
<tr> <td rowspan="6" style="padding-left:15px;"><img src="css/images/groupperms.gif" width="50" height="50"><td></tr>
<tr>
<td colspan="2" height="50"></td>
</tr>
<tr>
<td nowrap><span class="textcolor1">Account No. </span></td>
<td><input type='text' name='username' style="border:#cacaca 2px solid; color:#000099;" value=''></td></tr>
<tr>
<td> <a class="uiTooltip" href="#" onMouseOver="ajax_showTooltip(window.event,'tooltip/help.html',this);return false" onMouseOut="ajax_hideTooltip()">Help</a></td>
<td><input name="login" type='submit' value='Login' style="width:80px; font-family:Arial, Helvetica, sans-serif; font-weight:bold; color:#FFFFFF; border:#000000 2px solid; cursor:pointer; background-color: #1b4585;" border="0" align="right" title="Login" onClick="return checkLogin();"/></td>
</tr>
</table>
</form>
<table width="630" border="0" cellspacing="2" cellpadding="2" align="center" bgcolor="#FFFFFF" bordercolor="#FFFFFF">
<tr>
<td width="80"></td>
<td><img src="images/right_3.gif"/></td>
<td><span class="textcolor"><strong><a href='https://www.desco.org.bd/index.php?page=internet-bill-payment' target="_blank" class="uiTooltip">Before, use this facility/services please read instructions...</a></strong></span></td>
</tr>
</table>
<p align="center" class="textcolor"><strong><a href='https://www.desco.org.bd' class="uiTooltip">Back Home</a></strong></p>
<table align="center" width="135" border="0" cellpadding="2" cellspacing="0" title="Click to Verify - This site chose VeriSign SSL for secure e-commerce and confidential communications.">
<tr>
<td width="135" align="center" valign="top"><script type="text/javascript" src="https://seal.verisign.com/getseal?host_name=www.desco.org.bd&size=S&use_flash=YES&use_transparent=YES&lang=en"></script><br />
<a href="http://www.verisign.com/ssl-certificate/" target="_blank" style="color:#000000; text-decoration:none; font:bold 7px verdana,sans-serif; letter-spacing:.5px; text-align:center; margin:0px; padding:0px;">ABOUT SSL CERTIFICATES</a></td>
</tr>
</table>
</div>
<div align="center" style="padding-top:10px;">
<CENTER><B>
Total Visits: 1</B></CENTER>
</div>
</DIV>
</DIV>
</DIV>
</DIV>
<DIV class=pageright></DIV></DIV>
<DIV class=pagebotshadow>
<DIV id=Footer>Copyright ? 2010 Dhaka Electric Supply Company Ltd. All rights reserved.</DIV>
</DIV>
</DIV>
</BODY>
</HTML>
basically below code is the form
基本上下面的代码是形式
<form NAME='login' METHOD='POST' ACTION='authentication.php'>
<table width="350" height="181"cellpadding='0' cellspacing='0' border='0' style="border:#e5e5e5 0px solid; BACKGROUND: url(css/images/top9.png) no-repeat left top;" align="center">
<tr> <td rowspan="6" style="padding-left:15px;"><img src="css/images/groupperms.gif" width="50" height="50"><td></tr>
<tr>
<td colspan="2" height="50"></td>
</tr>
<tr>
<td nowrap><span class="textcolor1">Account No. </span></td>
<td><input type='text' name='username' style="border:#cacaca 2px solid; color:#000099;" value=''></td></tr>
<tr>
<td> <a class="uiTooltip" href="#" onMouseOver="ajax_showTooltip(window.event,'tooltip/help.html',this);return false" onMouseOut="ajax_hideTooltip()">Help</a></td>
<td><input name="login" type='submit' value='Login' style="width:80px; font-family:Arial, Helvetica, sans-serif; font-weight:bold; color:#FFFFFF; border:#000000 2px solid; cursor:pointer; background-color: #1b4585;" border="0" align="right" title="Login" onClick="return checkLogin();"/></td>
</tr>
</table>
</form>
i am trying this
我正在尝试这个
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);
}
}
but im getting below error
但我低于错误
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)
am is missing something? how to fix it?
我错过了什么?如何解决?
采纳答案by Alessandro Suglia
The URL that are you using in order to do the POST request is wrong, simply because when you have to do a specific request to a form you should use the web page that is present in the form tag, in this case "authentication.php".
您用于执行 POST 请求的 URL 是错误的,仅仅是因为当您必须对表单执行特定请求时,您应该使用表单标记中存在的网页,在这种情况下为“authentication.php ”。
So the code will be:
所以代码将是:
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);
}
}
This one correctly retrieves the web page that you want.
这个正确地检索到您想要的网页。
回答by Rajon Ali
For me the problem was that I didn't send the viewstate, eventvalidation, viewstategenerator values.
对我来说,问题是我没有发送 viewstate、eventvalidation、viewstategenerator 值。
To get these values you have to first send a GET request to the login form page. In my instance this was a default.aspx page.
要获取这些值,您必须首先向登录表单页面发送 GET 请求。在我的例子中,这是一个 default.aspx 页面。
Then you have to extract those values and put them into variables. Also of course you need the form login and password field ids, the submit button's id, among other things. For a list of all those variable that are sent, login manually and use chrome dev tools (inspect elements) to look at the network tab for a POST request. Inside it should show the username and password you submitted. There you will see other variables that are sent.
然后你必须提取这些值并将它们放入变量中。当然,您还需要表单登录名和密码字段 id、提交按钮的 id 等。要获取所有发送的变量的列表,请手动登录并使用 chrome 开发工具(检查元素)查看网络选项卡以查找 POST 请求。它里面应该显示您提交的用户名和密码。在那里您将看到发送的其他变量。
network tab in dev tools example
Next, send a POST request including all those variables, save the cookies from the response into a variable, and you can then use that to go to another page.
接下来,发送包含所有这些变量的 POST 请求,将响应中的 cookie 保存到变量中,然后您可以使用它转到另一个页面。
Code:
代码:
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<String, String> cookies = response.cookies();
Document homePage = Jsoup.connect("anotherpage")
.cookies(cookies)
.get();
System.out.println(homePage.body().html());
}
}
回答by WesternGun
In my case, it consists in three steps:
就我而言,它包括三个步骤:
- Get cookies from login form page;
- Post credentials to login form
action=
URL, renew cookies; - The user will be redirected to index page. Don't parse here, just send
Response
to the final target URL. - Parse when you reach the final page.
- 从登录表单页面获取cookies;
- 将凭据发布到登录表单
action=
URL,更新 cookie; - 用户将被重定向到索引页面。这里不解析,直接发送
Response
到最终目标 URL。 - 到达最后一页时进行解析。
The cookies should be renewed in every step, sometimes add entries, and sometimes you must clear it and reset. Be sure to check the network transmission with Internet Explorer/Chrome/Firefox Dev tool (F12), in the Network panel, cookies
part.
cookie 应在每一步中更新,有时添加条目,有时您必须清除并重置。请务必使用 Internet Explorer/Chrome/Firefox 开发工具 ( F12) 在网络面板中检查网络传输cookies
。