Jsoup无法登录页面

时间:2013-10-02 10:13:03

标签: jsoup

我遇到了一个问题:我无法使用Jsoup在页面上登录Betfair似乎没问题,但是没有得到记录页面的返回:(

// You can try with this username and password for testing
// Username: <redacted>
// Password: <redacted>
// LoginUrl: lite.betfair.com/Login.do?s=000009z-redirectDefault


// This is my Code 

Connection.Response res = Jsoup.connect("https://lite.betfair.com/SLoginsubmit.do?s=000009z-redirectDefault&secure=true")
                    .data("username", "<redacted>", "password", "<redacted>")
                    .method(Method.POST)
                    .execute();
            Map<String, String> cookies = res.cookies();


            Connection connection = Jsoup.connect("https://lite.betfair.com/Mybets.do?s=000209z");
            for (Entry<String, String> cookie : cookies.entrySet()) {
                connection.cookie(cookie.getKey(), cookie.getValue());
            }


            Document document = connection.get();
            System.out.println(document);

谁能帮帮我?

3 个答案:

答案 0 :(得分:2)

您必须连接到登录页面并将其cookie用于post命令。像这样:

    Connection.Response response1 = Jsoup.connect("https://lite.betfair.com/Login.do?s=000009z-redirectDefault")
            .execute();
    Map<String, String> cookies = response1.cookies();

    Connection connection2 = Jsoup.connect("https://lite.betfair.com/SLoginsubmit.do?s=000009z-redirectDefault&secure=true")
               .data("username", "<redacted>")
               .data("password", "<redacted>")
               .method(Method.POST);

    for (Entry<String, String> cookie : cookies.entrySet()) {
        connection2.cookie(cookie.getKey(), cookie.getValue());
    }
    Response response2 = connection2.execute();
    cookies.putAll(response2.cookies());

    Connection connection3 = Jsoup.connect("https://lite.betfair.com/Mybets.do?s=000209z");
    for (Entry<String, String> cookie : cookies.entrySet()) {
        connection3.cookie(cookie.getKey(), cookie.getValue());
    }

    Document document = connection3.get();
    System.out.println(document);

我使用你的代码连接另一个页面,它第一次工作。所以你帮助了我,我试着帮助你。 :)

答案 1 :(得分:2)

只需要知道cookie或SessionName的名称是什么,然后就可以使用它来登录

Response res = Jsoup.connect("https://lite.betfair.com/SLoginsubmit.do?s=000009z-rredirectDefault&secure=true")
                .method(Method.GET)
                .timeout(10000)
                .execute();

        sessionID = res.cookie("JSESSIONID");//her put the SessionName for website 

现在您拥有网站的SessionName,您需要填写它

String username="your username";
String password="your pass";

Jsoup.connect("https://lite.betfair.com/SLoginsubmit.do?s=000009z-redirectDefault&secure=true")
                .data("login:username", username, "login:password", password, "login:loginImg", "", "login", "login")
                .cookie("JSESSIONID", sessionID)
                .method(Method.POST)
                .timeout(10000)
                .execute();// now you have SessionName and you can use it for any page in website


Document doc = Jsoup.connect("https://lite.betfair.com/Mybets.do?s=000209z")
                .cookie("JSESSIONID", sessionID)
                .timeout(10000)
                .get();// her to open any page with SessionName you have it

现在你只需要在doc中标记你需要它来从中获取数据

System.out.println(doc.body().text());

答案 2 :(得分:0)

注意:网站的会话名称你可以这样知道: 登录到您的网站,您需要知道它的会话名称或cookie 然后在登录后在URL字段中写入此命令

javascript:void(alert(document.cookie))

然后获取会话名称