使用Jsoup提交登录表单时出现问题 [英] Problems submitting a login form with Jsoup
问题描述
由于某些原因,当我使用正确的登录信息时,此代码不会让我进入网站。 System.out.println
发布登录页面的代码,表明我的代码无法工作。有人可以告诉我我忘记了什么或者有什么问题吗?
public void connect(){
尝试{
Connection.Response loginForm = Jsoup.connect(https://www.capitaliq.com/CIQDotNet/Login.aspx/login.php)
.method(Connection.Method .GET)
.execute();
org.jsoup.nodes.Document document = Jsoup.connect(https://www.capitaliq.com/CIQDotNet/Login.aspx/authentication.php)
.data( cookieexists,false)
.data(username,myUsername)
.data(password,myPassword)
.cookies(loginForm.cookies() )
.post();
System.out.println(document);
} catch(IOException ex){
Logger.getLogger(WebCrawler.class.getName())。log(Level.SEVERE,null,ex);
}
}
用户名
,密码
和 Cookie
,网站要求两个额外的登录值 - VIEWSTATE
和 EVENTVALIDATION
。
您可以从第一个获取
请求的响应,像这样 -
Document doc = loginForm.parse();
Element e = doc.select(input [id = __ VIEWSTATE])。first();
String viewState = e.attr(value);
e = doc.select(input [id = __ EVENTVALIDATION])。first();
String eventValidation = e.attr(value);
然后在密码
(订单并不重要) -
org.jsoup.nodes.Document document =(org.jsoup.nodes.Document) Jsoup.connect(https://www.capitaliq.com/CIQDotNet/Login.aspx/authentication.php).userAgent(Mozilla / 5.0)
.data(myLogin $ myUsername,MyUsername )
.data(myLogin $ myPassword,MyPassword)
.data(myLogin $ myLoginButton.x,22)
.data(myLogin $ myLoginButton.y ,8)
.data(__ VIEWSTATE,viewState)
.data(__ EVENTVALIDATION,eventValidation)
.cookies(loginForm.cookies())
。 post();
我也会添加 userAgent
字段到这两个请求 - 一些网站测试它并发送不同的页面到不同的客户端,所以如果你想获得与你的额头相同的响应ser,添加请求 .userAgent(Mozilla / 5.0)
(或者您使用的任何浏览器)。
userName
的字段名称是 myLogin $ myUsername $ c $ c>,密码是 myLogin $ myPassword
和 Post
请求还包含关于登录按钮的数据。我无法测试它,因为我没有用户在该网站,但我相信它会起作用。希望这能解决你的问题。
编辑2
启用记住我
字段在登录过程中,将此行添加到发布
请求中:
.data (myLogin $ myEnableAutoLogin,on)
For some reason this code will not let me into the website when I use the correct login information. The System.out.println
posts the code of the login page, indicating my code did not work. Can someone tell me what I'm forgetting or what's wrong with it?
public void connect() {
try {
Connection.Response loginForm = Jsoup.connect("https://www.capitaliq.com/CIQDotNet/Login.aspx/login.php")
.method(Connection.Method.GET)
.execute();
org.jsoup.nodes.Document document = Jsoup.connect("https://www.capitaliq.com/CIQDotNet/Login.aspx/authentication.php")
.data("cookieexists", "false")
.data("username", "myUsername")
.data("password", "myPassword")
.cookies(loginForm.cookies())
.post();
System.out.println(document);
} catch (IOException ex) {
Logger.getLogger(WebCrawler.class.getName()).log(Level.SEVERE, null, ex);
}
}
解决方案 Besides the username
, password
and the cookies
, the site requeires two additional values for the login - VIEWSTATE
and EVENTVALIDATION
.
You can get them from the response of the first Get
request, like this -
Document doc = loginForm.parse();
Element e = doc.select("input[id=__VIEWSTATE]").first();
String viewState = e.attr("value");
e = doc.select("input[id=__EVENTVALIDATION]").first();
String eventValidation = e.attr("value");
And add it after the password
(the order doesn't really matter) -
org.jsoup.nodes.Document document = (org.jsoup.nodes.Document) Jsoup.connect("https://www.capitaliq.com/CIQDotNet/Login.aspx/authentication.php").userAgent("Mozilla/5.0")
.data("myLogin$myUsername", "MyUsername")
.data("myLogin$myPassword, "MyPassword")
.data("myLogin$myLoginButton.x", "22")
.data("myLogin$myLoginButton.y", "8")
.data("__VIEWSTATE", viewState)
.data("__EVENTVALIDATION", eventValidation)
.cookies(loginForm.cookies())
.post();
I would also add the userAgent
field to both requests - some sites test it and send different pages to different clients, so if you would like to get the same response as you get with your browser, add to the requests .userAgent("Mozilla/5.0")
(or whatever browser you're using).
Edit
The userName
's field name is myLogin$myUsername
, the password is myLogin$myPassword
and the Post
request also contains data about the login button. Ican't test it, because I don't have user at that site, but I believe it will work. Hope this solves your problem.
EDIT 2
To enable the remember me
field during login, add this line to the post
request:
.data("myLogin$myEnableAutoLogin", "on")
这篇关于使用Jsoup提交登录表单时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!