Download File With Webclient or Httpclient

How to download Excel file with HttpClient instead of WebClient in .NET?

The best source of documentation on HttpClient is, of course, the Microsoft site itself:
https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient
Here's an (oversimplified) version of the code I use for downloading spreadsheets:

private async Task SaveFile(string fileUrl, string pathToSave)
{
// See https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient
// for why, in the real world, you want to use a shared instance of HttpClient
// rather than creating a new one for each request
var httpClient = new HttpClient();

var httpResult = await httpClient.GetAsync(fileUrl);
using var resultStream = await httpResult.Content.ReadAsStreamAsync();
using var fileStream = File.Create(pathToSave);
resultStream.CopyTo(fileStream);
}

Problems downloading file using HttpClient

It seemed like the webserver hosting the files didnt like not having the User-Agent attribute set.
After setting it, the request worked.

C# Download file from webservice

You can try to use HttpClient instead. Usually, it is more convenient.

        var client = new HttpClient();
var response = await client.GetAsync(@"http://localhost:9000/api/file/GetFile?filename=myPackage.zip");

using (var stream = await response.Content.ReadAsStreamAsync())
{
var fileInfo = new FileInfo("myPackage.zip");
using (var fileStream = fileInfo.OpenWrite())
{
await stream.CopyToAsync(fileStream);
}
}

Downloading file without direct link through C# Webclient

It's important to note that the Webclient class uses the RETR command to download an FTP resource. For an HTTP resource, the GET method is used. That means if you provide a URL that doesn't contains the correct parameters to a downloadable file, you gonna end up with some exceptions that are not handled because Webclient was replaced with System.Net.Http.HttpClient, that I recommend you use instead.

Below you can see a exemple of how the Webclient works, on your case you are getting "useless error" because you are on a async method. I would suggest to use the normal method like below to debug and get the correct exception.

public void downloadProgrambyURL(string url, string filename)
{
using (WebClient client = new WebClient())
{
var pathVariable = "%USERPROFILE%\\Downloads\\" + filename;
var filePath = Environment.ExpandEnvironmentVariables(pathVariable);
client.DownloadFile(url, filePath);
}
}

Also, I'll let this link for your reference.

How to download a file from TLS1.3 enabled web site on Windows 10 using webclient or httpclient in C#

Finally I was successful in doing so, using following HttpClient but it is not applicable to all sites.

var httpClient = new System.Net.Http.HttpClient();
var httpResult = httpClient.GetAsync(fileUrl);
httpResult.Wait();
using (var resultStream = httpResult.Result.Content.ReadAsStreamAsync())
{

using (var fileStream = File.Create(filePath +"/"+ fileName))
{
resultStream.Result.Seek(0, SeekOrigin.Begin);
resultStream.Result.CopyTo(fileStream);
fileStream.Close();
}
}
return pathHMT + fileName2;

Although it work for the above link, its not working for another link
Australian Sanctions List

How do I specify the destination to download the file using httpclient

Never use anything but HttpClient. If you catch yourself typing WebClient of anything other than HttpClient, kindly slap your hand away from the keyboard.

You want to download a file with HttpClient? Here is an example of how to do that:

private static readonly HttpClient _httpClient = new HttpClient();

private static async Task DoSomethingAsync()
{
using (var msg = new HttpRequestMessage(HttpMethod.Get, new Uri("https://www.example.com")))
{
msg.Headers.Add("x-my-header", "the value");
using (var req = await _httpClient.SendAsync(msg))
{
req.EnsureSuccessStatusCode();
using (var s = await req.Content.ReadAsStreamAsync())
using (var f = File.OpenWrite(@"c:\users\andy\desktop\out.txt"))
{
await s.CopyToAsync(f);
}
}
}
}

You can do anything you want with HttpClient. No reason to use RestClient, WebClient, HttpWebRequest or any of those other "wannabe" Http client implementations.



Related Topics



Leave a reply



Submit