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?

wattostudios
  • 8,666
  • 13
  • 43
  • 57
124697
  • 22,097
  • 68
  • 188
  • 315
  • 1
    "Yes". It's all just HTTP GET requests. –  May 26 '12 at 23:22
  • 1
    (Once an HttpResponse is obtained, after an "execute", the HttpEntity, which has a stream that can be read from, is accessible. [See the API](http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/HttpClient.html) which provides a trivial example of just this). –  May 26 '12 at 23:28
  • I would prefer to Jsoup instead. – Sorter Oct 21 '13 at 16:13

2 Answers2

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.
}
Matt
  • 11,523
  • 2
  • 23
  • 33
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.

Francis Upton IV
  • 19,322
  • 3
  • 53
  • 57