1

您好我正在尝试使用htmlunit登录到服务器。当我禁用htmlunit'sjavascript 支持时,我的程序成功发布了登录信息,但将我重定向到另一个页面,上面写着“因为您没有使用 javascript,所以您必须按一次继续”。即使在普通浏览器中,自动单击此继续按钮也会导致 404 页面,因此这不是一个选项。

所以——我需要设置 javascript 才能执行身份验证。有人可以帮我吗?下面是我的代码、抛出的错误的堆栈跟踪以及我正在使用的 html 源代码。提前致谢。

    WebClient wc = new WebClient(BrowserVersion.FIREFOX_3_6);
    wc.getCookieManager().setCookiesEnabled(true);
    wc.setJavaScriptEnabled(true);

    HtmlPage page0 = wc.getPage("https://my.usc.edu/wp/shiblogin.do?r=/wp/student/searchForm.do");
    HtmlPage page1 = wc.getPage("https://shibboleth.usc.edu/idp/Authn/UserPassword");

    HtmlForm loginForm = page1.getForms().get(0);

    //HtmlSubmitInput button = loginForm.getInputByValue("Login");
    HtmlTextInput userField = loginForm.getInputByName("j_username");
    HtmlPasswordInput passwordField = loginForm.getInputByName("j_password");

    userField.setValueAttribute("username");
    passwordField.setValueAttribute("password");

    HtmlPage page2 = (HtmlPage) loginForm.getInputByValue("Login").click();

    String htmlBody = page2.getWebResponse().getContentAsString();

    System.out.println(page2.asText());
    System.out.println(htmlBody);

Caused by: com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException: 404 >/wp/student/searchForm.do/index.html for >https://my.usc.edu/wp/student/searchForm.do/index.html
>   at >com.gargoylesoftware.htmlunit.WebClient.throwFailingHttpStatusCodeExceptionIfNecessary(WebCli>ent.java:543)
>   at com.gargoylesoftware.htmlunit.WebClient.download(WebClient.java:2228)
>   at >com.gargoylesoftware.htmlunit.javascript.host.html.HTMLFormElement.submit(HTMLFormElement.jav>a:310)
>   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>   at >sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>   at java.lang.reflect.Method.invoke(Method.java:597)
>   at net.sourceforge.htmlunit.corejs.javascript.MemberBox.invoke(MemberBox.java:137)
>   ... 34 more
>== CALLING JAVASCRIPT ==
>function () {
>   [native code, arity=0]
>}

>======= EXCEPTION END ========

<form id="loginform" action="/idp/Authn/UserPassword" method="post" onsubmit="if (this.getAttribute('submitted')) return false; this.setAttribute('submitted','true'); document.getElementById('submit').disabled=true;" >

                <fieldset>
                    <legend>USCnet Login</legend>
                    <table id="login">
                        <tr>
                            <th><label for="j_username">USC NetID:</label></th>
                            <td><input id="j_username" name="j_username" type="text" maxlength="12" value="" /></td>
                        </tr>
                        <tr>
                            <th><label for="j_password">Password:</label></th>
                            <td><input id="j_password" name="j_password" type="password" /></td>
                        </tr>
                        <tr>
                            <td></td>
                            <td><input id="submit" type="submit" value="Login" /></td>
                        </tr>
4

0 回答 0