我正在尝试使用HtmlUnit提交表单,但不幸的是,在尝试了几乎所有内容之后,我获得了填充字段的相同页面。我希望另一个页面上有一个表格,其中包含我需要处理的信息。当我使用普通的网络浏览器时,我确实得到了它。显然有些事情我做得不对。请帮助!
final WebClient webClient = new WebClient(BrowserVersion.CHROME_16, "172.16.33.226", 8080);
webClient.setRefreshHandler(new RefreshHandler() {
public void handleRefresh(Page page, URL url, int arg) throws IOException {
System.out.println("handleRefresh");
}
});
webClient.getOptions().setRedirectEnabled(true);
webClient.getCookieManager().setCookiesEnabled(true);
webClient.getOptions().setCssEnabled(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
final HtmlPage page = webClient.getPage("http://221.120.224.197/ovportal/jsp/security/obs_login_html.jsp");
System.out.println(page.getTitleText());
final HtmlForm nform = page.getFormByName("nform");
final HtmlForm pwform = page.getFormByName("pwform");
HtmlSubmitInput button;
final HtmlTextInput textField;
final HtmlPasswordInput passField;
final HtmlHiddenInput hidden;
textField = nform.getInputByName("J_USERNAME");
这不是真正的用户名。
textField.setValueAttribute("******");
hidden = pwform.getInputByName("J_USERNAME");
passField = pwform.getInputByName("J_PASSWORD");
这不是真正的密码。
passField.setValueAttribute("******");
button = pwform.getInputByValue("Login");
pwform.getSelectByName("Timezone").setSelectedAttribute("PLT", true);
java.util.List<HtmlOption> list = pwform.getSelectByName("Timezone").getSelectedOptions();
for (HtmlOption option : list){
System.out.println(option.toString());
}
System.out.println("Before click " + hidden.getValueAttribute() + " " + textField.getValueAttribute() + ": ");
HtmlPage page2 = (HtmlPage) button.click();
System.out.println(page2.getUrl().toString());
System.out.println("After click " + hidden.getValueAttribute() + " " + textField.getValueAttribute() + ": "
+ passField.getValueAttribute() + ": " + page2.getTitleText() + "\n" + page2.asText());
String htmlContent = page2.asXml();
java.io.File htmlFile = new java.io.File("C:/index.html");
java.io.PrintWriter pw = new java.io.PrintWriter(htmlFile);
pw.print(htmlContent);
pw.close();
java.util.List<HtmlAnchor> anchors = page2.getAnchors();
for (HtmlAnchor anch : anchors) {
System.out.println(anch.getHrefLangAttribute() + "*.*" + anch.getHrefAttribute());
}
webClient.closeAllWindows();