JSOUP无法保持登录状态

时间:2018-08-24 08:35:20

标签: java login jsoup

当前下面的代码将起作用,因为println确认确实确实登录了我:

        final String USER_AGENT = "\"Mozilla/5.0 (Windows NT\" +\n" +
                "          \" 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2\"";
        HashMap<String, String> cookies = new HashMap<>();
        HashMap<String, String> formData = new HashMap<>();
        Connection.Response loginForm = Jsoup.connect("********************/login/").method(Connection.Method.GET).userAgent(USER_AGENT).timeout(7000).validateTLSCertificates(false).execute();
        Document loginDoc = loginForm.parse();

        cookies.putAll(loginForm.cookies());

        String form_key = loginDoc.select("input[type=\"hidden\"]").first().attr("value");  // save the cookies, this will be passed on to next request
        formData.put("login[username]", "***********");
        formData.put("login[password]", "****");
        formData.put("form_key", form_key);
        formData.put("send", "");

        Connection.Response homePage = Jsoup.connect("*****************.com/loginPost/").cookies(cookies).data(formData).method(Connection.Method.POST).userAgent(USER_AGENT).timeout(7000).validateTLSCertificates(false).execute();
        System.out.println(homePage.parse().html());

添加另一行时,该程序不会保持登录新网站的位置。

        Connection.Response homePage2 = Jsoup.connect(url).cookies(cookies).data(formData).method(Connection.Method.POST).userAgent(USER_AGENT).timeout(7000).validateTLSCertificates(false).execute();
        System.out.println(homePage2.parse().html());

我的主要目的是在登录时以此收集信息:

        Document doc = Jsoup.connect(url).userAgent(USER_AGENT).timeout(2500).cookies(cookies).data(formData).validateTLSCertificates(false).method(Connection.Method.POST).get();
        return doc.select(".dealer-price").text();

当然也不起作用。我不确定如何从这里开始,是jsoup的问题还是我可以解决的问题? TIA

0 个答案:

没有答案