HttpClient.GetStringAsync() is slow
Hey I wanted to download some gifs from a wiki page via a console app.
Everything works and all, but when it comes to downloading the images, it's kinda slow. So here's the crucial part
Granted, I'm doing some really slow things here like two requests, I/O and Regex on a huge string, but assuming the server is 100% responsive, it shouldn't take 10s for one iteration. Not sure what's happening in the background
9 Replies
Why are you downloading a gif as a string..?
.GetByteArrayAsync()
No, I'm getting the source code of the page first because the path doesn't have a predictable pattern so I'm extracting it from the source code
Ah, yeah, I see that
I'd use the profiler, or at least some logging with time, to figure out what takes the most time
Something I noticed while writing the post is that I can replace
File.WriteAllBytes
with the async version of it.My bet is on the regex
Might be worth a try
The regex is actually one of the faster instructions (2ms). The two main culprits are the two requests with about 450ms
Maybe the assumption is wrong that the server is responsive all the time because i cannot find any major flaw in my code
It could be rate-limiting you
How long does the request take from the browser? From Postman or something?
Could you try spoofing the user-agent to say it's Chrome or Firefox?
have you tried using
Task.WhenAll
to parallelise this?@HimmDawg load the page in your browser and open up the devools. That can show you how long getting the html part of the pages takes. If it's about the same as in your code then there's nothing to be done.