1

我刚开始使用 HtmlUnit,我想登录一个使用 Javascript 的 sHttp 页面,有问题吗?我希望没有,但我无法登录,问题出在哪里?至少我应该收到登录失败的警告,但一切似乎都没有尝试登录。非常感谢每一个想法!

WebClient webClient = new WebClient();
//webClient.setJavaScriptEnabled(true);
//webClient.getCookieManager().setCookiesEnabled(true);


// Get the first page
HtmlPage page1 = (HtmlPage) webClient.getPage("http://goo.gl/AxhP8");
HtmlPage page2 = (HtmlPage) page1.getFrameByName("text").getEnclosedPage();

// Get the form that we are dealing with and within that form,
// find the submit button and the field that we want to change.
HtmlForm form = page2.getFormByName("login");

// Enter login and passwd
form.getInputByName("xlogin").setValueAttribute("login");
form.getInputByName("xpasswd").setValueAttribute("password");


// Click "Sign In" button/link
page1 = (HtmlPage) form.getInputByValue("Přihlásit").click();

//  Get page as Html
String htmlBody = page1.getWebResponse().getContentAsString();
//  Save the response in a file
String filePath = "test_out.html";

BufferedWriter bw = new BufferedWriter(new FileWriter(new File(filePath)));
bw.write(htmlBody);
bw.close();

webClient.closeAllWindows();

这是表格(或者您可以在此处获取来源:http: //goo.gl/AxhP8

<form action="/fcgi/verso.fpl" method="GET" name="login" onsubmit="wipRet=true;wipRet=true;wipRet=kontrola_hesla(this);if(wipRet==false){wipRet=false;}else{vrGetScrollTop(this);doWkinPrgs(this.target,'Probíhá přihlášení uživatele ...');};if(wipRet==false){return false;}else{vrGetScrollTop(this);doWkinPrgs(this.target,'');}" target="text">
<input name="_vr_sclt" type="hidden" value="0" />
<input name="_vr_sclt" type="hidden" value="0" />
<input id="fname" name="fname" type="hidden" value="neni_js" />

Uživatel: <input class="login_in"  name="xlogin" size="15" type="text" />
Heslo: <input class="login_in" name="xpasswd" size="15" type="Password" />
<input class="btn" type="submit" value="Přihlásit" width="50" />

<input name="__formid" type="hidden" value="2a620f43d0478d14" />
<input name="__idf" type="hidden" value="afc09868294ba6ac" />
<input name="__formid" type="hidden" value="f73fa3ddfe457bc1" />
<input name="__idf" type="hidden" value="3162299a379cf323" />

</form>

感谢每一个小小的帮助,我花了几个小时,阅读了很多文章,但我无法继续前进,谢谢 :) cookie 或类似的东西有问题吗?

4

1 回答 1

0
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);

Firefox 比 IE 好得多 ...:// 现在可以使用

于 2012-12-02T13:40:20.680 回答