Is it possible to download files like PDF with HttpClient?

code511788465541441 picture code511788465541441 · May 27, 2012 · Viewed 19.1k times · Source

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?

Answer

Matt picture Matt · May 27, 2012

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.
}