Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to download files like PDF with HttpClient?

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?

like image 318
code511788465541441 Avatar asked May 26 '12 23:05

code511788465541441


People also ask

What is HttpClient API?

An HttpClient can be used to send requests and retrieve their responses. An HttpClient is created through a builder . The builder can be used to configure per-client state, like: the preferred protocol version ( HTTP/1.1 or HTTP/2 ), whether to follow redirects, a proxy, an authenticator, etc.


1 Answers

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.
}
like image 128
Matt Avatar answered Oct 11 '22 20:10

Matt