使用JSoup登录URL(Java)

使用JSoup登录URL(Java),java,jsoup,Java,Jsoup,我正试图以通常的方式登录一个网页,以浏览/浏览数据。登录部分一切正常,但我得到的响应是HTML页面,上面写着“正在登录,请稍候” 我想要的返回页是“序列”中的最后一页 有没有办法跳过这个?我错过了什么 抱歉,如果这是重复的,我已经阅读了StackOverflow,但没有找到类似的内容 代码如下所示: public static void main(String[] args) throws IOException, ParseException{ final String USER_AG

我正试图以通常的方式登录一个网页,以浏览/浏览数据。登录部分一切正常,但我得到的响应是HTML页面,上面写着“正在登录,请稍候”

我想要的返回页是“序列”中的最后一页

有没有办法跳过这个?我错过了什么

抱歉,如果这是重复的,我已经阅读了StackOverflow,但没有找到类似的内容

代码如下所示:

public static void main(String[] args) throws IOException, ParseException{
    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\"";
    String username = "xxx";
    String password = "xxx";
    HashMap<String, String> cookies = new HashMap<>();
    HashMap<String, String> formData = new HashMap<>();
    String loginFormUrl = "https://id.ice.no/oauth2/account/login?returnUrl=%2Foauth2%2Fconnect%2Fauthorize%2Fcallback%3Fclient_id%3DSelfService%26redirect_uri%3Dhttps%253A%252F%252Fminside.ice.no%252Fsignin-callback.html%26response_type%3Did_token%2520token%26scope%3Dopenid%2520profile%2520roles%2520SelfService%26state%3D7f6047df0ddd4949b2992761ed98dd3b%26nonce%3Dbfc111c39548438c9a39b327c745947f%26acr_values%3DreturnUrl%2520Lw%253D%253D";
    String loginActionUrl= "https://id.ice.no/oauth2/account/login?returnUrl=%2Foauth2%2Fconnect%2Fauthorize%2Fcallback%3Fclient_id%3DSelfService%26redirect_uri%3Dhttps%253A%252F%252Fminside.ice.no%252Fsignin-callback.html%26response_type%3Did_token%2520token%26scope%3Dopenid%2520profile%2520roles%2520SelfService%26state%3D6ef2cad2efd24b3db7d61343aacc29f5%26nonce%3Db8b29eea53ae4952b8ffec5c43a9882a%26acr_values%3DreturnUrl%2520L2Fib25uZW1lbnQvMTYxODYwNjIvc2VuZHNtcw%253D%253D";
    Connection.Response loginForm = Jsoup.connect(loginFormUrl).method(Connection.Method.GET).userAgent(USER_AGENT).execute();
    Document loginDoc = loginForm.parse(); // this is the document that contains response html
    cookies.putAll(loginForm.cookies()); // save the cookies, this will be passed on to next request

    formData.put("username", username);
    formData.put("Password", password);
    String authToken = loginDoc.select("#form > input[type=hidden]:nth-child(8)")
      .first()
      .attr("value");
    formData.put("__RequestVerificationToken", authToken);

    Connection.Response homePage = Jsoup.connect(loginActionUrl)
   .cookies(cookies)
   .data(formData)
   .method(Connection.Method.POST)
   .userAgent(USER_AGENT)
   .execute();


   System.out.println(homePage.parse().html());

如果没有真正的登录名和密码来检查那里发生了什么,很难说,所以我不得不猜测。几句话:

用户名为的输入具有名称username,但您正在使用username设置formData。这可能会引起问题

登录表单只有很少的输入,但您只需要输入用户名、密码和令牌。这可能会引起问题。您应该始终包括所有字段,因此您缺少:

最好使用浏览器的调试器查看真正提交的数据:

如果可以的话没问题,但我不喜欢你怎么拿到代币。而不是: 您可以简单地使用:

String authToken = loginDoc.select("input[name=__RequestVerificationToken]").first().attr("value");
这可能是最重要的。我希望有一个页面可以将浏览器重定向到ReturnUrl中定义的URL。Jsoup无法处理javascript重定向,因此您必须手动请求该页面。事实上,你的浏览器无论如何都会这么做——用最新的cookies发出另一个请求。您所处的轨道是正确的,因此在代码末尾,您还应该通过获得的cookie获得您想要的页面: 编辑:
哦,这些确实是正确的证件。但我在Chrome调试器的“网络”选项卡中看到了更多正在进行的操作和更多的重定向。这很复杂,因为新URL的一部分是由Javascript生成的。要克服Jsoup的局限性,请尝试使用Selenium Webdriver。

共享您的代码,以便有人能够帮助您
String authToken = loginDoc.select("#form > input[type=hidden]:nth-child(8)").first().attr("value");
String authToken = loginDoc.select("input[name=__RequestVerificationToken]").first().attr("value");
 cookies.putAll(homePage.cookies());// get the cookies after successful login
 Connection.Response finalPage = Jsoup.connect(returnUrl) //this should be the URL of the page you want to visit in the first place
   .cookies(cookies)
   .userAgent(USER_AGENT)
   .execute();