ghostopened on Aug 16, 2019 · GitHub

I'm trying to automate some file download (using HtmlUnit) from a website that is based on liferay+struts+angular. HtmlUnit has done a fine job in navigating pages and interpreting JS so far, but when I try to download the file, it returns me the same page and not the attachment.

The download button is made as such:

<button class="btn" name="filedownload" data-ng-disabled="false"
        type="button" data-ng-lick="vm.downloadFile(...things...)">
</button>

I've already tried with the two main techniques that I've found online: using a CollectingAttachmentHandler and .getWebResponse().getContentAsStream(), both of them give me the starting page and not the attachment.

By tracing it with the network tracer of Chrome I see that once I clicked it makes a call to 2 different endpoints, the first to trace that such file will be doenloaded and the second to download the file, the second one has a response with the correct Content-Disposition: attachment; filename= header, but apparently HtmlUnit is not picking it up.

Here is some snippet of the code that I've tried.

CollectingAttachmentHandler attachmentHandler = new
CollectingAttachmentHandler();
client.setAttachmentHandler(attachmentHandler);
attachmentHandler.handleAttachment(myButton.click());
// I get only one attachment, the page
List<Attachment> attachments = attachmentHandler.getCollectedAttachments();
// In my_file I find the page
try (InputStream in = myButton.click().getWebResponse().getContentAsStream();
     OutputStream out = Files.newOutputStream(new File(""...my_file...).toPath())) {
    IOUtils.copy(in, out);
}

I've also debugged the NicelyResynchronizingAjaxController and I see the two abovementhioned calls passing, both seem to be processed correctly, but I don't get the file back; I've also tried the latest 2.36.0 SNAPSHOT which does not solve the problem.

My client is configured as follows:

final WebClient client = new WebClient(BrowserVersion.CHROME);
client.setAjaxController(new NicelyResynchronizingAjaxController());
client.setCssErrorHandler(new SilentCssErrorHandler());
client.getCache().clear();
client.getCookieManager().clearCookies();
client.setJavaScriptTimeout(90000);
final WebClientOptions options = client.getOptions();
options.setCssEnabled(true);
options.setJavaScriptEnabled(true);
options.setDoNotTrackEnabled(true);
options.setDownloadImages(false);
options.setGeolocationEnabled(false);
options.setScreenWidth(1920);
options.setScreenHeight(1080);
options.setTimeout(90000);
options.setSSLClientCipherSuites(
    new String[] { "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
        "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
        "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384",
        "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256" });
options.setUseInsecureSSL(false);
options.setPopupBlockerEnabled(true);
options.setRedirectEnabled(true);
options.setAppletEnabled(false);
options.setThrowExceptionOnFailingStatusCode(false);
options.setThrowExceptionOnScriptError(false);
options.setPrintContentOnFailingStatusCode(true);

Read the original on github.com ↗