使用Jsoup发布登录数据

时间:2013-04-19 22:05:46

标签: java android post web-scraping jsoup

我正在尝试登录此网站:http://deeproute.com

这是我的代码。

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

不幸的是,这只是在未登录状态下返回同一页面。我做错了什么?

2 个答案:

答案 0 :(得分:4)

您需要在发布前阅读表格!你缺少param subbera = Login。


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

}

答案 1 :(得分:0)

可能是一些(实际上很多)几个月太晚但我发现我必须做一些网站报废,其中网站设计真的令人困惑(我需要从中提取数据的网站)。还必须先登录才能获得cookie信息。 @ MariuszS的答案很有帮助,但唯一的问题是,我不确定预期的地图/键值对应该是什么。感谢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 +'")'
}

之前将“str”的值附加到Jsoup请求中
.method(Connection.Method.GET)
.execute();

希望这证明对我来说有点帮助:)