HtmlUnit下载pdf

时间:2017-10-06 19:05:30

标签: java htmlunit

我正在尝试使用HtmlUnit v.2.27下载PDF。

我尝试了不同的方法,附件,添加了WebWindowListener ......但我无法下载PDF文件。

有任何线索如何做到这一点?

网络和测试数据的详细信息在网络上进行了硬编码。

     public void start() throws Exception {

    try {

     String folioNumber = "278E1D22C5747F961";
     String rut = "21759297-6";

        try (final WebClient webClient = new WebClient(BrowserVersion.BEST_SUPPORTED)) {

            webClient.getOptions().setJavaScriptEnabled(false);
            webClient.getOptions().setThrowExceptionOnScriptError(false);
            webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
            webClient.getOptions().setActiveXNative(true);
            webClient.getOptions().setAppletEnabled(true);
            webClient.getOptions().setPrintContentOnFailingStatusCode(true);
            webClient.setAjaxController(new NicelyResynchronizingAjaxController());

            final HtmlPage home = webClient.getPage("https://www.afpmodelo.cl/AFP/Centro-de-ayuda/Herramientas/Validacion-Certificados.aspx");

            HtmlInput rutInput = home.getFirstByXPath("//*[@id=\"ContentPlaceHolder1_txt_RUT\"]");
            rutInput.setValueAttribute(rut);

            HtmlInput folioInput = home.getFirstByXPath("//*[@id=\"ContentPlaceHolder1_txt_FOLIO\"]");
            folioInput.setValueAttribute(folioNumber);

            HtmlImage captchaImg = home.getFirstByXPath("//*[@id=\"imgCaptcha\"]");

            File imageFile = new File("/tmp/AfpModeloSpider" + LocalDateTime.now().toString() + "captcha.jpg");
            captchaImg.saveAs(imageFile);               

            HtmlInput captchaInput = home.getFirstByXPath("//*[@id=\"ContentPlaceHolder1_txtCaptcha\"]");
            captchaInput.setValueAttribute(captchaResult);

            HtmlInput validarButton = home.getFirstByXPath("//*[@id=\"ContentPlaceHolder1_btn_VALIDAR_CERT\"]");

            HtmlPage result = validarButton.click();
            webClient.waitForBackgroundJavaScript(6000);

            saveFile(result.getWebResponse().getContentAsStream());             


        }
    } catch (Exception e) {
        throw e;
    }
}

0 个答案:

没有答案
相关问题