Downloader icon indicating copy to clipboard operation
Downloader copied to clipboard

Replace HttpWebRequest with HttpClient

Open rogerfar opened this issue 4 years ago • 10 comments
trafficstars

I came across this issue recently: https://stackoverflow.com/questions/66156483/webrequest-behavour-different-between-net-framework-4-8-and-dotnet-5 and https://twitter.com/mikaelsyska/status/1359968961765535744

It might be worth looking into replacing the HttpWebRequest in ChunkDownloader with something like a HttpClient, which has been completely rewritten in .NET Core and should yield better performance.

rogerfar avatar Feb 11 '21 22:02 rogerfar

similar to https://github.com/bezzad/Downloader/issues/31 And yes, I agree with that😁

ghost1372 avatar Feb 12 '21 06:02 ghost1372

I'm researching and I hope I can find a solution soon. Because the structure cost of the program for conversion and full use of HttpClient is expensive.

bezzad avatar Feb 12 '21 08:02 bezzad

I think HttpWebRequest can be replaced with HttpRequestMessage, the later one is used by HttpClient and almost the same with HttpWebRequest. Also note that HttpClient should be used as singleton (it won't release socket even after calling Dispose()).

hez2010 avatar Aug 23 '21 03:08 hez2010

There is a problem with http client. Buffer of HttpClient is limited to 2GB. Won't it make problems?

GihanSoft avatar Oct 10 '21 13:10 GihanSoft

@GihanSoft You can buffer it by yourself while using HttpClient:

var req = new HttpRequestMessage(...);
var res = (await httpClient.SendAsync(req, HttpCompletionOption.ResponseHeadersRead)).EnsureSuccessStatusCode();
using var stream = await res.Content.ReadAsStreamAsync();
const int bufferSize = 1048576;
var buffer = new byte[bufferSize];
while (true)
{
    var length = await stream.ReadAsync(buffer);
    if (length == 0) break;
    // use buffer[0..length]
}

hez2010 avatar Oct 10 '21 13:10 hez2010

@hez2010 The point is you can't control flow of date. no way to pause it or slow it. If you set low buffer capacity, it won't wait for buffer to be read. It just throw.

GihanSoft avatar Oct 14 '21 06:10 GihanSoft