I've been stuck for three weeks with solving a problem in C#. Namely, not so long ago I noticed on the language documentation site that the WebClient class is not recommended to use, it is better to use HttpClient instead.
This is a good suggestion, but when I started using it, I noticed that it was impossible to get progress on the execution of the process, that is, if I download a file, I will not know how much was downloaded/there was also other technical information that was easily available in WebClient. So here's the question. There is such a piece of code and I can't understand why progressMessageHandler does not output information about the download process and if this is not the best way to get information about the download, what can you advise WITHOUT WebClient?..
string textDownloadProgress;
private async void DownloadFileAsync()
{
string filename = "file.zip";
var handler = new HttpClientHandler() { AllowAutoRedirect = true };
var ph = new ProgressMessageHandler(handler);
var hm = new HttpRequestMessage() { RequestUri = new Uri("URL") };
var client = new HttpClient(ph) { Timeout = Timeout.InfiniteTimeSpan };
var progressMessageHandler = new ProgressMessageHandler(new HttpClientHandler());
progressMessageHandler.HttpReceiveProgress += (_, e) =>
{
textDownloadProgress += e.ProgressPercentage;
};
using (var filestream = new FileStream(filename, FileMode.Create))
{
var netstream = await client.GetStreamAsync(hm.RequestUri);
await netstream.CopyToAsync(filestream);
}
}
I will be very grateful for your help, I have spent a lot of time and effort, but I have not found a decent solution…