How to get web authentication page

I use the web client to get the page source. I successfully logged in. After that, I use the same object to get the page source using a different URL, but it shows an exception like:

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

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

above code works successfully and login After that I use this code

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

But I get an exception

java.lang.ClassCastException: com.gargoylesoftware.htmlunit.UnexpectedPage cannot be cast to com.gargoylesoftware.htmlunit.html.HtmlPage
+5
source share
2 answers

The JavaDoc of UnexpectedPage says that

A shared page that is returned whenever an unexpected content type is returned by the server.

I would advise you to check the content type. webClient.getPage("url");

+1

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

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

.

0

All Articles