我正在嘗試爲網站下載xls文件。當我點擊鏈接下載文件時,我得到一個javascript確認框。我如下處理它使用HtmlUnit下載文件
ConfirmHandler okHandler = new ConfirmHandler(){
public boolean handleConfirm(Page page, String message) {
return true;
}
};
webClient.setConfirmHandler(okHandler);
有一個鏈接到下載文件。
<a href="./my_file.php?mode=xls&w=d2hlcmUgc2VsbElkPSd3b3JsZGNvbScgYW5kIHN0YXR1cz0nV0FJVERFTEknIGFuZCBkYXRlIDw9IC0xMzQ4MTUzMjAwICBhbmQgZGF0ZSA%2BPSAtMTM1MDgzMTU5OSA%3D" target="actionFrame" onclick="return confirm('Do you want do download XLS file?')"><u>Download</u></a>
我點擊使用
HTMLPage x = webClient.getPage("http://working.com/download");
HtmlAnchor anchor = (HtmlAnchor) x.getFirstByXPath("//a[@target='actionFrame']");
anchor.click();
handeConfirm()方法被excuted的鏈接。但我不知道如何從服務器保存文件流。我試圖用下面的代碼查看流。
anchor.click().getWebResponse().getContentAsString();
但是,結果與頁面x相同。任何人都知道如何從服務器捕獲流?謝謝。
'''anchor.click()'將返回一個頁面。這應該contian你的XLS文件 – Lee
看到我的答案類似的問題在http://stackoverflow.com/a/28471835/612123 – culmat