C#
C#

help

Root Question Message

Error101
Error1013/12/2023
❔ I'm trying to download a link in 4 chunks via HttpClient but it keeps breaking.

Link to my GitRepository https://github.com/Error1OH1/FileDownloader
I've been trying to download in 4 chunks but I can't seem to get the file to download in 4 chunks properly it loops through but always ends up only being a quarter of the download, and this new updated code just breaks but downloads a 0 KB file.
Damien.
Damien.3/12/2023
You can do this to do a c sharp codeblock

```cs

```
Damien.
Damien.3/12/2023
makes it easier to read in discord
Damien.
Damien.3/12/2023
couldnt get it to work yet but
Damien.
Damien.3/12/2023
I did see this
Damien.
Damien.3/12/2023
i used a snippet of ur code to show u
Damien.
Damien.3/12/2023
I put answers with comments in that pull request
Damien.
Damien.3/12/2023
🙂
tebeco
tebeco3/12/2023
you want to re-write everything from the ground
tebeco
tebeco3/12/2023
starting from the csproj
tebeco
tebeco3/12/2023
including all blocking code
tebeco
tebeco3/12/2023
get read of WebRequest
tebeco
tebeco3/12/2023
create ONLY ONE httpclient per remote domain
tebeco
tebeco3/12/2023
stop using (using var = new... on HttpClient
tebeco
tebeco3/12/2023
everything
tebeco
tebeco3/12/2023
your PR should not fix anything with ConcurrentBag<DownloadChunk> chunks = new ConcurrentBag<DownloadChunk>();
tebeco
tebeco3/12/2023
because the new happen inside the function
tebeco
tebeco3/12/2023
there's nothing parallilized in that method with array or conceurrentbag
tebeco
tebeco3/12/2023
the caller is ... but it was and is still isolated in a single call
tebeco
tebeco3/12/2023
the issue is literally all the code above and around
Error101
Error1013/12/2023
I am okay with rewriting it for the sake of learning but could you explain why first
tebeco
tebeco3/12/2023
WebRequest is dead since years
tebeco
tebeco3/12/2023
stop using it 😄
tebeco
tebeco3/12/2023
HttpClient replaced it
tebeco
tebeco3/12/2023
that should be the only explanation for that line
tebeco
tebeco3/12/2023
now
tebeco
tebeco3/12/2023
HttpClient + Dipose can create issue in the long run
Error101
Error1013/12/2023
Oh I used that because I was having a hard time understanding how to use httpclient and was basing it off a friends
tebeco
tebeco3/12/2023
issue with how TCP works
tebeco
tebeco3/12/2023
so re-use the httpclient for the same remote host
tebeco
tebeco3/12/2023
in your case I have no clue if you're a webapp or a console
tebeco
tebeco3/12/2023
if you;re a console ... use static
Error101
Error1013/12/2023
What do you mean
Error101
Error1013/12/2023
It's a console application if that is what you mean
tebeco
tebeco3/12/2023
stop calling new HttpClient everytime
Error101
Error1013/12/2023
I'm still new to programming
Error101
Error1013/12/2023
Does this cause corruption if I do?
tebeco
tebeco3/12/2023
if all the request hit the same server ... create one and re-use
tebeco
tebeco3/12/2023
if request goes to 3 server, create 3 and re-use per URL
Error101
Error1013/12/2023
OH I THINK I GET WHAT YOU MEAN
Error101
Error1013/12/2023
IN MY CLASS USE THE SAME FOR ALL THE METHODS
tebeco
tebeco3/12/2023
scheme://subdomain.domain.tld/path/subpath?query <==== you want 1 httpclietn per scheme://subdomain.domain.tld
Error101
Error1013/12/2023
SO CALL IT OUTSIDE OF THE METHODS AS PUBLIC USE
tebeco
tebeco3/12/2023
let's say the remote server if named Foo
tebeco
tebeco3/12/2023
you generally create a dedicated class
tebeco
tebeco3/12/2023
just for speaking Http with Foo
Error101
Error1013/12/2023
I was trying to create an HttpClient Library class so I could use it for future projects easier
Error101
Error1013/12/2023
Is that what broke it?
tebeco
tebeco3/12/2023
public class FooHttpClient
{
  private static HttpClient _httpClient = new();

  public async Task<Something> GetSomethingAsync()
  {
    await _httpClient.Get.......Async(.......);
  }
}
tebeco
tebeco3/12/2023
or even:
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy