C
C#2w ago
Core

✅ Downloading file with HttpClient does not work

Hello, Everywhere it is advisable to use HttpClient for downloading larger files. For some reason it only downloads a little chunk of a larger file, and that's it. How should I even determine what the problem is? The url is fine, I am able to download the file trough the browser
c#
await using var stream = await httpClient.GetStreamAsync(_url);
await using var fileStream = new FileStream(filePath, FileMode.Create);
await stream.CopyToAsync(fileStream);
c#
await using var stream = await httpClient.GetStreamAsync(_url);
await using var fileStream = new FileStream(filePath, FileMode.Create);
await stream.CopyToAsync(fileStream);
4 Replies
becquerel
becquerel2w ago
do you still see this when you use methods like httpClient.GetBytesAsync()? or is it exclusively when working with it as a stream? perhaps the site could also be sniffing your user-agent and working differently based on that - that's pure speculation, but worth a passing thought
Core
CoreOP2w ago
Yes, unfortunately it's the same with GetBytes. Actually, the website specifically hands out an access token and an URL that exposes the file I guess it's not about the user-agent also I've exhausted my daily download limit, now I'm trying to download from Dropbox.... But the issue was the same when I tried to download from the original site
becquerel
becquerel2w ago
my next step in that case would be to see if you can make the request succesfully outside of c#, i.e. in Postman or something similar as a way to isolate if it's the http request that's not working or the HttpClient specifically thinking on it, this could also be caused by chunked transfer encoding i've not been bitten by that before so i don't know if you need to do anything special with httpclient, but worth looking into
Core
CoreOP2w ago
Thank you for helping me
Want results from more Discord servers?
Add your server