成功登录JSoup后无法解析网站 [英] Unable to parse website after successful log in JSoup

查看:141
本文介绍了成功登录JSoup后无法解析网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JSoup库成功登录了网站,存储了cookie,以便在需要访问的第二个文档中使用它们.但是,第二个文档将解析数据,就像我没有登录一样.

I successfully log into website using JSoup library, store the cookies so that i can use them for the second document where access is required. However the second document parses the data as if i am not logged in.

这是代码:

public class Main {

public static void main(String[] args) throws Exception {

    Map<String, String> loginCookies = null;


     Connection.Response loginForm = Jsoup.connect("login page")
             .method(Connection.Method.GET)
             .execute();

     loginCookies = loginForm.cookies();

     Document document = Jsoup.connect("login page")
             .data("cookieexists", "false")
             .data("username", "user")
             .data("password", "pass")
             .data("loginbtn", "Log in")
             .cookies(loginCookies)
             .post();


    Document document2 = Jsoup.connect("Page with access required")
        .cookies(loginCookies)
        .get();

    System.out.println(document2);
}

}

此代码可能有什么问题?

What can be wrong with this code?

推荐答案

您应该存储和重复使用包含有关会话信息的cookie,这意味着您需要存储它们的身份,而不是仅仅通过传递凭据后获得的服务器响应从空的形式.

You should store and reuse cookies which contain information about your session, which means you need to store them from server response which you get after passing your credentials, not just from empty form.

所以尝试

Connection.Response loginForm = Jsoup.connect("login page")
        .data("cookieexists", "false")
        .data("username", "user")
        .data("password", "pass")
        .data("loginbtn", "Log in")
        //.cookies(loginCookies)
        .method(Connection.Method.POST)
        .execute();

//here `loginForm` connected to server with your credentials
//and server returned response with cookies containing informations
//required to continue session so you should store them
//and reuse to access farther pages
Map<String, String> loginCookies  = loginForm.cookies();

Document document2 = Jsoup.connect("Page with access required")
        .cookies(loginCookies)
        .get();

System.out.println(document2);

这篇关于成功登录JSoup后无法解析网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆