The Apache HttpClient library makes accessing and downloading HTTP data easy. This used to be standalone but is now under the Apache. Follow us on.
![Java Httpclient Download File Example Java Httpclient Download File Example](/uploads/1/2/5/8/125860593/867898131.png)
Downloading content to a local file is a common thing to do. The current version of HttpClient doesn’t yet provide out of the box support for saving content to a file but this sample shows how to extend HttpClient with new ways of reading content retrieved using HttpClient. Please see List of ASP.NET Web API and HttpClient Samples for the complete sample solution.
The HttpContent class contains content to be sent to a client (in the case of PUT, POST, etc.) as well as data being read from the server in a response. The basic System.Net.Http NuGet package provides support for reading the content as a stream, a string, or a byte array using one of
- HttpContent.ReadAsStringAsync
- HttpContent.ReadAsStreamAsync
- HttpContent.ReadAsByteArrayAsync
The way to extend how an HttpContent can be consumed is through the ReadAs* extension methods. The System.Net.Formatter NuGet package offers a set of additional ReadAs* methods, for reading and deserializing data at the same time. This sample shows how to add a simple ReadAsFileAsync extension method but the floor is open for any number of ways of reading the content.
![Java httpclient post file Java httpclient post file](/uploads/1/2/5/8/125860593/586685133.png)
In general, when you read data from an HttpContent it is consumed meaning that it can’t be read again (like when you read a non-seekable stream). However, if you want to be able to read the content multiple times then you can use the LoadIntoBufferAsync method to do that. This will cause the content to get read into an internal buffer so that it can consumed multiple times without retrieving it again over the network.
First we make the ReadAsFileAsync extension method on HttpContent to provide support for reading the content and storing it directly in a local file:
Finally we put the two together – in this case we download an image from Google Maps and open it up in the default image viewer (if you don’t have an image viewer then opening the downloaded image will fail but that doesn’t change the download part):
Have fun!
Henrik
del.icio.us Tags: asp.net,webapi,mvc,rest,httpclient
Synopsis
This repo is the second step of Create Health Monitoring and emergency aid system by implementing android application to communicate with the serversystem.Please note that only the client part is covered in this Repo — you need the Server Repo to have the complete system.Please see our Server Repo.
Setup
Setup is straightforward, it is similar to every other Android Application.
- run
git clone https://github.com/MohamedSeliem/HttpClient.git
in your local machine. - import the Project to your Android Studio.
- build the project and run the Application
Screen Shots
Collaborators
Some useful links:
Git -