4

I am using web client for getting page source. I have logged in successfully. After that, I use same object for getting page source using different URL but it's showing an Exception like:

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

This is the code which i am 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 running successfully and login After that i am using this code

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

But i am getting Exception

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

2 回答 2

1

UnexpectedPage的JavaDoc内他们声明

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

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

于 2012-06-25T14:12:14.337 回答
0

而不是使用

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

利用

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

它对我很好。

于 2013-06-23T10:48:36.427 回答