Java 下载HTMLUnit中的wav文件

Java 下载HTMLUnit中的wav文件,java,html,audio,htmlunit,Java,Html,Audio,Htmlunit,在有人告诉我这里已经有这个问题之前,我必须说我已经尝试了我找到的每一个例子 我试图下载的url有一种“音频/wav”,嵌入在视频标签中,或者至少这是我在运行Chrome的element inspector时看到的 问题是,我不能在这里发布的URL不是指向.wav文件或任何东西,而是指向一个ASP页面,它似乎生成了音频 到目前为止还不错,这里的问题是我不能真正下载音频 基本上,我的webclient是这样创建的: WebClient webClient = new WebClient(Browse

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

我试图下载的url有一种“音频/wav”,嵌入在视频标签中,或者至少这是我在运行Chrome的element inspector时看到的

问题是,我不能在这里发布的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.
}

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

我已经解决了这个问题很久了,然后只是想让其他人知道。
解决方案是放弃HTMLUnit并将Selenium与phamtomJS结合使用。

我能够使用HTMLUnit获得音频元素
仅供参考,我的版本是2.15,在FireFox中,音频在标签中,而在Chrome中,它似乎使用自己的影子Dom来播放文件。我发现主页生成了一个cookie,然后用于生成音频,但在HTML单元中,用于生成音频的cookie不存在。。。或者至少没有出现在webClient.getCookieManager.getCookies列表中;有什么原因吗?