2012-12-02 79 views
1

我刚刚开始使用HtmlUnit,我想登录到使用Javascript的sHttp页面,这是一个问题吗?我希望不会,但我无法登录,问题在哪里?至少我应该得到不成功登录的警告,但似乎没有尝试登录。非常感谢每一个想法!无法通过HtmlUnit/Java登录

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> 

感谢每一点帮助,我花了几个小时,看了很多文章,我不能够移动转发,thx :)有没有问题,饼干或类似的东西?

回答

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

火狐比IE更好的...://现在的工作