在 HTMLUnit 中下载 wav 文件

Download wav files in HTMLUnit

在有人告诉我这里已经有这个问题之前,我必须说我基本上已经尝试了我找到的每一个例子。

我正在尝试下载的 url 类型为 'audio/wav',嵌入在视频标签中,或者至少这是我在 运行 [=35 时看到的=]的元素检查器。

问题是,URL(我不能在此处 post)不指向 .wav 文件或任何内容,而是指向 ASP 页面,该页面似乎生成了音频。

到目前为止一切顺利,这里的问题是我无法真正下载音频。

基本上我的网络客户端是这样创建的:

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 写入控制台,它 returns 主页源,例如:

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。

这个问题我已经解决很久了,就让大家知道吧。 解决方案是放弃 HTMLUnit 并使用带有 phamtomJS 的 Selenium。

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