且构网

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

使用JSoup登录页面的困难

更新时间:2023-12-03 21:18:16

几件事:

  1. GET请求-像在POST请求中一样添加user agent字符串始终是一个好习惯:

  1. The GET request - it's always a good practice to add the user agent string like you did at the POST request:

Connection.Response loginForm = Jsoup.connect("https://pro-labs.imdb.com/login")
        .method(Connection.Method.GET)
        .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0")
        .execute();

  • 随机的"appToken"-您必须从对上一个GET请求的响应中将其提取-您不能使用随机或旧值:

  • The random "appToken" - you must extract it from the response to the previous GET request - you cannot use random or old value:

    Document doc = loginForm.parse();
    Element e = doc.select("input[name=appToken]").first();
    String appToken = e.attr("value");
    

  • POST请求-您必须添加服务器发送给您的会话cookie以及对GET请求的响应-

  • The POST request - you must add the session cookies that the server sends you with the response to the GET request -

    Document res = Jsoup.connect("https://pro-labs.imdb.com/login")
        .data("appToken", appToken)
        .data("emailAddress", "*****@*****.***")
        .data("password", "*****")
        .userAgent("Mozilla")
        .cookies(loginForm.cookies())
        .post();