JSOUP自动登录网站

时间:2016-12-18 13:55:19

标签: java login jsoup

我正在尝试解析需要登录的网站。在浏览器中,当我在登录页面(https://backoffice.holidayinsider.com/backoffice2/login)登录时,我会自动重定向到https://backoffice.holidayinsider.com/backoffice2/login

首先,我尝试使用Java自动化登录。我的方法是由stackoverflow response推动的。我正在使用的代码如下:

package Login;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
import java.util.Map;
public class DoLogin {
    public static void main(String[] args)
    {

        try {

            Connection.Response loginForm = Jsoup.connect("https://backoffice.holidayinsider.com/backoffice2/login")
                .method(Connection.Method.GET)
                .execute();

            Connection.Response mainPage = Jsoup.connect("https://backoffice.holidayinsider.com/backoffice2")
                .data("username", "myusername")
                .data("password", "mypass")
                .cookies(loginForm.cookies()).execute();
            System.out.println(mainPage.parse());

            Map<String, String> cookies = mainPage.cookies();

            Document evaluationPage = Jsoup.connect("https://backoffice.holidayinsider.com/backoffice2/")
                .cookies(cookies)
                .execute().parse();
            System.out.println(evaluationPage);


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

似乎登录正在运行,因为状态代码是200.现在问题是当我打印mainPage时,它似乎仍然在登录页面。可能是什么原因?提前感谢您的任何帮助或建议。

1 个答案:

答案 0 :(得分:1)

登录时需要添加POST方法:

        Connection.Response loginForm = Jsoup.connect("https://backoffice.holidayinsider.com/backoffice2/login")
            .method(Connection.Method.GET)
            .execute(); 

        Connection.Response mainPage = Jsoup.connect("https://backoffice.holidayinsider.com/backoffice2/login")
            .data("username", "myusername") 
            .data("password", "mypass") 
            .cookies(loginForm.cookies())
            .followRedirects(true)
            .method(Connection.Method.POST).execute(); 
        System.out.println(mainPage.parse());

        Map<String, String> cookies = mainPage.cookies();

        Document evaluationPage = Jsoup.connect("https://backoffice.holidayinsider.com/backoffice2/")
            .cookies(cookies)
            .get();
        System.out.println(evaluationPage);