java 使用 Jsoup POST 登录数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16114189/
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
Using Jsoup to POST login data
提问by user1945153
I'm trying to log into this website: http://deeproute.com
我正在尝试登录此网站:http: //deeproute.com
This is my code.
这是我的代码。
Connection.Response res = null;
Connection homeConnection = null;
Document homePage = null;
Map<String, String> loginCookies = null;
try {
res = Jsoup.connect("http://www.deeproute.com/")
.data("cookieexists", "false")
.data("name", user)
.data("password", pswd).method(Method.POST)
.execute();
} catch (IOException e) {
e.printStackTrace();
}
if (res != null) {
loginCookies = res.cookies();
try {
homePage = Jsoup.connect("http://www.deeproute.com")
.cookies(loginCookies).get();
} catch (IOException e) {
e.printStackTrace();
}
Unfortunately, this simply returns the same page in a not-logged-in state. What am I doing wrong?
不幸的是,这只是在未登录状态下返回相同的页面。我究竟做错了什么?
采纳答案by MariuszS
You need to read form before posting! You are missing param subbera=Login.
你需要在发帖前阅读表格!您缺少参数 subbera=登录。
public static void main(String[] args) throws Exception {
Connection.Response loginForm = Jsoup.connect("http://deeproute.com/deeproute/default.asp")
.method(Connection.Method.GET)
.execute();
Document document = Jsoup.connect("http://deeproute.com/deeproute/default.asp")
.data("cookieexists", "false")
.data("name", "username")
.data("password", "pass")
.data("subbera", "Login")
.cookies(loginForm.cookies())
.post();
}
回答by Eesa Jacobs
Might be a few(many actually) months too late but i found that i had to do some web scrapping where the site was designed really confusing (the site i needed to extract data from). Also had to login first to get cookie info. @MariuszS's answer was helpful but the only problem was, i wasn't sure what the expected Map/Key-Value-Pairs were supposed to be. Thanks to Javascript, i quickly retrieved the form keys and values and was able to login successfully. Here's the Javascript:
可能已经晚了几个月(实际上很多),但我发现我不得不在网站设计真正令人困惑的地方进行一些网页抓取(我需要从中提取数据的网站)。还必须先登录才能获取 cookie 信息。@MariuszS 的回答很有帮助,但唯一的问题是,我不确定预期的 Map/Key-Value-Pairs 应该是什么。多亏了 Javascript,我很快就检索到了表单键和值,并且能够成功登录。这是Javascript:
var str = "";
//the form selector i.e.
//that which is to be submitted to. In
//my case, i needed to submit the entire body
var arr = $("input[name]");
for(var i = 0, l = arr.length; i<l; i++){
str+= '.data("'+ arr[i].name +'", "'+ arr[i].value +'")'
}
Append the value of "str" to your Jsoup request before
将“str”的值附加到您的 Jsoup 请求之前
.method(Connection.Method.GET)
.execute();
Hope this proves to be somewhat helpful like it was for me :)
希望这证明对我有点帮助:)