简体   繁体   中英

Download PDF using Apache HttpClient

I'm writing a program to download all of my monthly statements from my ISP using HttpClient. I can login to the site, access pages, and download pages but I can't download my PDF statements. It just downloads some HTML. I used the answer to this question to start with. Here is my method where I'm trying to download the 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) {

    }

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