HttpClient is a simple and robust wrapper to send and receive HTTP requests. It's an awesome alternative to the legacy HTTP client .NET api. I like HttpClient the best. It's free, efficient and especially easy to use. You can send HTTP requests and then receive data back only with a couple of code lines.
For instance:
A function to download file with a provided uri and output path.
public static class HttpHelper
{
private static readonly HttpClient _httpClient = new HttpClient();
public static async void DownloadFileAsync(string uri
, string outputPath)
{
Uri uriResult;
if (!Uri.TryCreate(uri, UriKind.Absolute, out uriResult))
throw new InvalidOperationException("URI is invalid.");
if (!File.Exists(outputPath))
throw new FileNotFoundException("File not found."
, nameof(outputPath));
byte[] fileBytes = await _httpClient.GetByteArrayAsync(uri);
File.WriteAllBytes(outputPath, fileBytes);
}
}
Hope you enjoy this post.
Happy coding :)
Top comments (4)
You should NEVER NEVER EVER put HttpClient inside
using
, you should reuse an existing var and keep it alive thought the lifetime of your app.docs.microsoft.com/en-us/dotnet/ap...
Thanks Alex :). You're right.
The DownloadFile function is just a sample function to demonstrate how to download file using HttpClient asynchronously.
In practice, the existing HttpClient var should be reused throughout the lifetime of the application.
I just updated this post.
Can be greatly improved by not loading the entire download into memory with GetByteArrayAsync. Instead open a stream and then send that stream into a file stream.
Nice and Helpfull Code. Regarding your file control existence, case file not exists you may create a new one, or it would be user default option...