有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java HtmlUnit表单重定向问题

我在一家网上银行应用程序公司工作。 我想登录以检索余额。我一直在做一些研究,从其他帖子中发现了一些有用的HtmlUnit代码。然而,我一直在处理page的重定向。路径如下: 主登录表单(第1页)生成验证凭据的新页面(第2页)。到目前为止,我做得很好,但我找不到检索主页(第3页)的解决方案,下面是代码:

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class WellsF {
    public static void main(String[] args) throws Exception {

        WebClient webClient = new WebClient(BrowserVersion.CHROME_16);
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setCssEnabled(true);
        webClient.getOptions().setUseInsecureSSL(true);
        webClient.getOptions().setRedirectEnabled(true);

        HtmlPage page1 = webClient
                .getPage("https://www.wellsfargo.com/home.jhtml");

        final HtmlForm form = (HtmlForm) page1.getElementById("frmSignon");

        form.getInputByName("userid").setValueAttribute("user id");
        form.getInputByName("password").setValueAttribute("user password");

        HtmlPage page2 = (HtmlPage) form.getInputByName("btnSignon").click();

        synchronized (page2) {
            page2.wait(5000);
            System.out.println(page2.asText());
            System.out.println("=========================== page2\n");
        }

        HtmlPage page3 = (HtmlPage) webClient.openWindow(page2.getUrl(),
                "signon").getEnclosedPage();
        System.out.println(page3.asText());
        webClient.closeAllWindows();
    }
}

共 (1) 个答案

  1. # 1 楼答案

    请尝试以下代码并检查其是否正常工作:

     HtmlPage page3 = (HtmlPage) webClient.getPage(page2.getUrl());
     webClient.getOptions().setTimeout(10*1000);