我正在使用Web客户端获取页面源。我已经成功登录。之后,我使用相同的对象使用不同的URL获取页面源,但是它显示如下异常:

java.lang.ClassCastException: com.gargoylesoftware.htmlunit.UnexpectedPage cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlPage

这是我正在使用的代码。
            forms = (List<HtmlForm>) firstPage.getForms();
        form = firstPage.getFormByName("");

        HtmlTextInput usernameInput = form.getInputByName("email");
        HtmlPasswordInput passInput = form.getInputByName("password");
        HtmlHiddenInput redirectInput = form.getInputByName("redirect");
        HtmlHiddenInput submitInput = form.getInputByName("form_submit");

        usernameInput.setValueAttribute(username);
        passInput.setValueAttribute(password);

        //Create Submit Button
        HtmlElement button = firstPage.createElement("button");
        button.setAttribute("type", "submit");
        button.setAttribute("name", "submit");
        form.appendChild(button);
        System.out.println(form.asXml());
        HtmlPage pageAfterLogin = button.click();

        String sourc = pageAfterLogin.asXml();

        System.out.println(pageAfterLogin.asXml());

    /////////////////////////////////////////////////////////////////////////

上面的代码成功运行并登录
之后,我使用此代码
    HtmlPage downloadPage = null;
downloadPage=(HtmlPage)webClient.getPage("url");

但我越来越例外
java.lang.ClassCastException: com.gargoylesoftware.htmlunit.UnexpectedPage cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlPage

最佳答案

JavaDoc of UnexpectedPage中,他们指出

当出现意外的内容类型时返回的通用页面
由服务器返回。

我建议您检查webClient.getPage("url");的内容类型

08-27 15:34