简体   繁体   English

使用Apache HttpClient下载PDF

[英]Download PDF using Apache HttpClient

I'm writing a program to download all of my monthly statements from my ISP using HttpClient. 我正在编写一个程序,使用HttpClient从ISP下载我的所有月度报表。 I can login to the site, access pages, and download pages but I can't download my PDF statements. 我可以登录该站点,访问页面和下载页面,但是不能下载我的PDF语句。 It just downloads some HTML. 它只是下载一些HTML。 I used the answer to this question to start with. 我从这个问题的答案开始。 Here is my method where I'm trying to download the PDF: 这是我尝试下载PDF的方法:

public void downloadPdf() throws ClientProtocolException, IOException  {
HttpGet httpget = new HttpGet("https://www.cox.com/ibill/PdfBillingStatement.stmt?account13=123&stmtCode=001&cycleDate=7/21/2014&redirectURL=error.cox");
    HttpResponse response = client.execute(httpget);

    System.out.println("Download response: " + response.getStatusLine());

    HttpEntity entity = response.getEntity();

    InputStream inputStream = null;
    OutputStream outputStream = null;

    if (entity != null) {
        long len = entity.getContentLength();
        inputStream = entity.getContent();

        outputStream = new FileOutputStream(new File("/home/bkurczynski/Desktop/statement.pdf"));

        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }

        outputStream.close();
    }
}

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thank you! 谢谢!

HttpClient httpClient = HttpClientBuilder.create().build();
    try {
        HttpGet request = new HttpGet("https://www.cox.com/ibill/PdfBillingStatement.stmt?account13=123&stmtCode=001&cycleDate=7/21/2014&redirectURL=error.cox");
        HttpResponse response = httpClient.execute(request);
        HttpEntity entity = response.getEntity();

        InputStream is = entity.getContent();
        String filePath = "hellow.txt";
        FileOutputStream fos = new FileOutputStream(new File(filePath));
        int inByte;
        while ((inByte = is.read()) != -1)
            fos.write(inByte);
        is.close();
        fos.close();

    } catch (Exception ex) {

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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