Java – how to use the web client to get data authenticated pages

I am using the web client to get the page source I have successfully logged in After that, I use the same object to obtain page sources with different URLs, but the exceptions displayed are as follows:

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

This is the code I'm using

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());

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

After the code runs successfully and logs in, I use this code

HtmlPage downloadPage = null;       
downloadPage=(HtmlPage)webClient.getPage("url");

But I got an exception

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

Solution

In the year of Javadoc of unexpected page, they explained this

I suggest you check webclient The content type of getpage ("URL");

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>