简体   繁体   English

使用 Jsoup POST 登录数据

[英]Using Jsoup to POST login data

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?我究竟做错了什么?

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

}

Might be a few(many actually) months too late but i found that i had to do some web scraping where the site was designed really confusing (the site i needed to extract data from).可能会晚几个月(实际上很多),但我发现我必须在网站设计真正令人困惑的地方(我需要从中提取数据的网站)进行一些网络抓取。 Also had to login first to get cookie info.还必须先登录才能获取 cookie 信息。 @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. @MariuszS 的回答很有帮助,但唯一的问题是,我不确定预期的 Map/Key-Value-Pairs 应该是什么。 Thanks to Javascript, i quickly retrieved the form keys and values and was able to login successfully.多亏了 Javascript,我很快就检索到了表单键和值,并且能够成功登录。 Here's the 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 :)希望这证明对我有点帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM