Download file using HttpClient wrapper asynchronously.

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 the…


This content originally appeared on DEV Community and was authored by 1001binary

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 :)


This content originally appeared on DEV Community and was authored by 1001binary


Print Share Comment Cite Upload Translate Updates
APA

1001binary | Sciencx (2021-04-16T08:46:10+00:00) Download file using HttpClient wrapper asynchronously.. Retrieved from https://www.scien.cx/2021/04/16/download-file-using-httpclient-wrapper-asynchronously/

MLA
" » Download file using HttpClient wrapper asynchronously.." 1001binary | Sciencx - Friday April 16, 2021, https://www.scien.cx/2021/04/16/download-file-using-httpclient-wrapper-asynchronously/
HARVARD
1001binary | Sciencx Friday April 16, 2021 » Download file using HttpClient wrapper asynchronously.., viewed ,<https://www.scien.cx/2021/04/16/download-file-using-httpclient-wrapper-asynchronously/>
VANCOUVER
1001binary | Sciencx - » Download file using HttpClient wrapper asynchronously.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/16/download-file-using-httpclient-wrapper-asynchronously/
CHICAGO
" » Download file using HttpClient wrapper asynchronously.." 1001binary | Sciencx - Accessed . https://www.scien.cx/2021/04/16/download-file-using-httpclient-wrapper-asynchronously/
IEEE
" » Download file using HttpClient wrapper asynchronously.." 1001binary | Sciencx [Online]. Available: https://www.scien.cx/2021/04/16/download-file-using-httpclient-wrapper-asynchronously/. [Accessed: ]
rf:citation
» Download file using HttpClient wrapper asynchronously. | 1001binary | Sciencx | https://www.scien.cx/2021/04/16/download-file-using-httpclient-wrapper-asynchronously/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.