C#C
C#4y ago
HimmDawg

HttpClient.GetStringAsync() is slow

Hey owo
So 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

for (int i = 0; i < names.Count(); i++)
{
      Console.Write($"Download gif ({progress} / {names.Count()})");

      string name = names[i];

      var pageSourceCode = await client.GetStringAsync($"{baseURL}/{name}_idle_animation.gif");
      Regex gifFinderRegex = new Regex(@"<a href=""(/images/[a-zA-Z0-9]/[a-zA-Z0-9]{2}/[a-zA-Z0-9]+_idle_animation\.gif)"">");

      Match match = gifFinderRegex.Match(pageSourceCode);

      byte[] fileBytes = await client.GetByteArrayAsync($"{baseURL}{match.Groups[1]}");
      File.WriteAllBytes(Path.Combine(saveLocation, name + ".gif"), fileBytes);

      progress++;
      Console.Write("\r");
}

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 foxThinking
Was this page helpful?