2012-12-02 81 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更好的...://現在的工作