在HTMLUnit中下载wav文件

时间:2015-07-29 13:46:32

标签: java html audio htmlunit

在有人告诉我这里已经存在这个问题之前,我必须说我已经尝试过我发现的每一个例子。

我尝试下载的网址有一种“音频/ wav”,嵌入在视频标签中,或者至少这是我在运行Chrome元素检查器时看到的。

问题是,URL(我不能在这里发布)不是指向.wav文件,也不指向任何内容,而是指向ASP页面,它似乎生成音频。

到目前为止一直很好,这里的问题是我无法真正下载音频。

基本上我的webclient创建如下:

WebClient webClient = new WebClient(BrowserVersion.FIREFOX_38); // Also tried Chrome here.
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setPopupBlockerEnabled(false);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
HtmlPage page = (HtmlPage)webClient.getPage(URL);

我尝试创建一个链接到包含音频文件的页面的锚元素:

HtmlElement createdElement = (HtmlElement) page.createElement("a");
createdElement.setAttribute("id", "link_som");
createdElement.setAttribute("href", "../sound.asp?app=audio");
page.appendChild(createdElement);

HtmlAnchor anc =(HtmlAnchor) page.getElementById("link_som", true); //tried this just to make sure it was returning the right anchor

InputStream inputStream = anc.click().getWebResponse().getContentAsStream();
//Writing the inputStream to a file generates a file which has 0 KB.

还尝试运行通过HtmlUnit链接到新URL的javascript:

ScriptResult resultado = page.executeJavaScript("window.open('../sound.asp?app=audio');");
webClient.waitForBackgroundJavaScript(5000);
HtmlPage paginaRes = (HtmlPage)resultado.getNewPage();

InputStream inputStream =paginaRes.getWebResponse().getContentAsStream(); //Here the inputStream also generates a 0 KB file

有趣的是,在我试过的所有情况下,如果我将inputStream写入控制台,它将返回主页面源,例如:

int binary = 0;
while ((binary = inputStream.read()) != -1)
{
   System.out.print((char)binary); //prints the old page source, and in some other tests, prints nothing.
}

Ps。:手动打开chrome上的URL时,它有一个嵌入式播放器,在FireFox上,它要求Quicktime。

2 个答案:

答案 0 :(得分:0)

我可以使用htmlunit来获取音频元素 仅供参考,我的版本是2.15

答案 1 :(得分:-1)

我已经解决了很久,然后只是为了让别人知道。 解决方案是放弃HTMLUnit并使用Selenium和phamtomJS。

相关问题