加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何使用Web客户端获取经过数据身份验证的页面

发布时间:2020-12-15 02:35:10 所属栏目:Java 来源:网络整理
导读:我正在使用Web客户端获取页面源.我已成功登录.之后,我使用相同的对象来获取使用不同URL的页面源,但它显示的异常如下: java.lang.ClassCastException: com.gargoylesoftware.htmlunit.UnexpectedPage cannot be cast to com.gargoylesoftware.htmlunit.html.
我正在使用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年,他们说明了这一点

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读