且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Jsoup登录

更新时间:2023-12-03 21:26:40

少量备注:

  • 您的HTTP状态为302.使用Jsoup.connect(...).followRedirects(true)
  • 是个好主意
  • 某些服务器会检查您来自的站点,因此建议设置引荐来源标头:Jsoup.connect(...).referrer(URL + "/login")
  • 您确定hidden值始终为69吗?也许每个请求都不同.您可以这样动态获取它:formData.put("hidden",html.select("co_js").first().attr("value"));
  • 我不喜欢您获取令牌的方式.让我们使用Jsoup提取它: String authToken = html.select("input#token").first().attr("value");

  • You're getting HTTP status 302. It may be a good idea to use Jsoup.connect(...).followRedirects(true)
  • Some servers check the site you're coming from so it's recommened to set referrer header: Jsoup.connect(...).referrer(URL + "/login")
  • Are you sure the hidden value is always 69? Maybe it's different for each request. You can get it dynamically like this: formData.put("hidden",html.select("co_js").first().attr("value"));
  • I don't like your way of getting the token. Let's use Jsoup to extract it: String authToken = html.select("input#token").first().attr("value");

修改:

通过让服务器知道您可以处理压缩的页面,可以减小下载的页面大小.对于每个请求,请使用:Jsoup.connect(...).header("accept-encoding", "gzip, deflate")它是透明的,您无需执行任何特殊处理即可,但它在内部起作用.

By letting server know you can handle compressed pages you can decrease downloaded page size. For every request use: Jsoup.connect(...).header("accept-encoding", "gzip, deflate") It's transparent and you don't have to do anything special to handle it, but it works internally.

编辑2 :

根据先前的建议提供最终解决方案:

Providing final solution based on previous advices:

    import java.util.HashMap;
    import java.util.Map;

    import org.jsoup.Connection;
    import org.jsoup.Connection.Response;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;

    public class ***51734840 {

        private static final String URL = "https://time2watch.in";
        private static final String URL_LOGIN = URL + "/login/";
        static String userAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36";

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

            Map<String, String> headers = new HashMap<String, String>();
            headers.put("accept-encoding", "gzip, deflate");

            Connection.Response loginForm = Jsoup.connect(URL + "/login").userAgent(userAgent).headers(headers).execute();
            Map<String, String> cookies = loginForm.cookies();
            Document html = loginForm.parse();

            String authToken = html.select("input#token").first().attr("value");
            System.out.println("Found authToken:" + authToken);

            Map<String, String> formData = new HashMap<String, String>();
            formData.put("username", "!!!!!!!!!!!!!!!!!!");
            formData.put("pwd", "!!!!!!!!!!!!!!!!!!");
            formData.put("hidden", "69");
            formData.put("token", authToken);
            headers.put("Content-Type", "application/x-www-form-urlencoded");

            System.out.println("cookies before login:");
            System.out.println(cookies);
            System.out.println(" Logged in cookie present? " + cookies.containsKey("s4icookuser"));

            Connection.Response afterLoginPage = Jsoup.connect(URL_LOGIN).cookies(cookies).headers(headers)
                    .userAgent(userAgent).data(formData).method(Connection.Method.POST).referrer(URL_LOGIN).execute();
            // update cookies
            cookies = afterLoginPage.cookies();

            System.out.println("cookies after login:");
            System.out.println(cookies);
            System.out.println(" Logged in cookie present? " + cookies.containsKey("s4icookuser"));

            Response homePage = Jsoup.connect(URL).cookies(cookies).method(Connection.Method.GET).userAgent(userAgent)
                    .referrer(URL_LOGIN).followRedirects(true).referrer(URL_LOGIN).headers(headers).execute();

            Document doc = homePage.parse();
            System.out.println("Error? " + doc.text().contains("Erreur"));
            System.out.println("OK? " + !doc.text().contains("Se connecter"));
            System.out.println("Logged in as: " + doc.select(".dropdown-toggle").text());
        }

    }