Is it possible to download files like PDF with HttpClient?
Asked Answered
R

2

8

I found some examples here on how to download a file but most of them seem to be using HttpURLConnection. is it possible to download files with HttpClient?

Riverhead answered 26/5, 2012 at 23:19 Comment(3)
"Yes". It's all just HTTP GET requests.Serology
(Once an HttpResponse is obtained, after an "execute", the HttpEntity, which has a stream that can be read from, is accessible. See the API which provides a trivial example of just this).Serology
I would prefer to Jsoup instead.Sensitivity
S
20

Using httpclient is pretty easy. Here's a link to it's tutorial.

http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e43

HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(urltofetch);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
    long len = entity.getContentLength();
    InputStream inputStream = entity.getContent();
    // write the file to whether you want it.
}
Suffruticose answered 27/5, 2012 at 3:47 Comment(0)
E
1

Anything you can do with HttpURLConnection you can do, usually better, with HttpClient look through their examples about file transfer and you will see how.

Erg answered 26/5, 2012 at 23:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.