简体   繁体   中英

How to download a ZIP file with HTMLUnit clicking on an anchor

I'm trying to download a ZIP file with HTMLUnit 2.32 using the following code.

I obtain a "myfile.zip" bigger than the one downloaded through a normal browser (179kb vs 79kb) and which is corrupt.

How one should click an anchor and download a file with HTMLUnit?

        WebClient wc = new WebClient(BrowserVersion.CHROME);

        final String HREF_SCARICA_CONSOLIDATI = "/web/area-pubblica/quotate?viewId=export_quotate";

        final String CONSOBBase = "http://www.consob.it";

        HtmlPage page = wc.getPage(CONSOBBase + HREF_SCARICA_CONSOLIDATI);

        final String downloadButtonXpath = "//a[contains(@href, 'javascript:downloadAzionariato()')]";
        List<HtmlAnchor> downloadAnchors = page.getByXPath(downloadButtonXpath);
        HtmlAnchor downloadAnchor = downloadAnchors.get(0);

        UnexpectedPage downloadedFile = downloadAnchor.click();

       InputStream contentAsStream = downloadedFile.getWebResponse().getContentAsStream();
        File destFile = new File("/tmp", "myfile.zip");
        Writer out = new OutputStreamWriter(new FileOutputStream(destFile));
        IOUtils.copy(contentAsStream, out);
        out.close();

Have updated your code snippet a bit to make it work. Hope the inline comments are helping a bit to understand what is going on (using the latest SNAPSHOT code of HtmlUnit (2.34-SNAPSHOT 2018/11/03)

final String HREF_SCARICA_CONSOLIDATI = "http://www.consob.it/web/area-pubblica/quotate?viewId=export_quotate";

try (final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_60)) {                                   
    HtmlPage page = webClient.getPage(HREF_SCARICA_CONSOLIDATI);                                               

    final String downloadButtonXpath = "//a[contains(@href, 'javascript:downloadAzionariato()')]";             
    List<HtmlAnchor> downloadAnchors = page.getByXPath(downloadButtonXpath);                                   
    HtmlAnchor downloadAnchor = downloadAnchors.get(0);                                                        

    // click does some javascript magic - have a look at your browser                                          
    // seems like this opens a new window with the content as response                                         
    // because of this we can ignore the page returned from click                                              
    downloadAnchor.click();                                                                                    
    // instead of we are waiting a bit until the javascript is done                                            
    webClient.waitForBackgroundJavaScript(1000);                                                               

    // now we have to pick up the window/page that was opened as result of the download                        
    Page downloadPage = webClient.getCurrentWindow().getEnclosedPage();                                        

    // and finally we can save to content                                                                      
    File destFile = new File("/tmp", "myfile.zip");                                                            
    try (InputStream contentAsStream = downloadPage.getWebResponse().getContentAsStream()) {                   
        try (OutputStream out = new FileOutputStream(destFile)) {                                              
            IOUtils.copy(contentAsStream, out);                                                                
        }                                                                                                      
    }                                                                                                          

    System.out.println("Output written to " + destFile.getAbsolutePath());                                     
}                                                                                                              

While RBRi considerations are interesting, I discovered my code worked with HTMLUnit 2.32 with no modifications but I was writing the file the wrong way!

I used

Writer out = new OutputStreamWriter(new FileOutputStream(destFile));
IOUtils.copy(contentAsStream, out);

while it had to be (no OutputStreamWriter)

FileOutputStream out = new FileOutputStream(destFile);
IOUtils.copy(contentAsStream, out);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM