2

I'm using an HttpClient and HttpResponseMessage to execute a lot of requests.

If I do responseMessage.Headers.GetValues("whatever"), this seem to take a LOT of time, reducing my program's speed by almost 200%.

Here's a minimal reproducible example:

var httpClient = new HttpClient();

using var requestMessage = new HttpRequestMessage(HttpMethod.Get, "https://api.example.com/x");

var responseMessage = await httpClient.SendAsync(requestMessage);

var accessToken = responseMessage.Headers.GetValues("X-Anything"); // Without this line, my program runs almost 200% faster

I am using .NET Core 3 on Windows 10 to run this code.

Why that? Is there a workaround? This is a real problem.

Elka
  • 21
  • 2
  • 3
    It would be awesome if you could share a [mcve]. Be sure to mention which specific version of .NET Core / Framework you are running, on what platform. – mjwills Nov 18 '19 at 10:29
  • May this help you: https://stackoverflow.com/questions/44682818/httpclient-on-webapi-is-extremely-slow – Ash Nov 18 '19 at 10:32
  • `this seem to take a LOT of time, reducing my program's speed by almost 200%.`. What exactly is a lot of time, and what is the baseline for this '200% speed reduction'? – Zdeslav Vojkovic Nov 18 '19 at 10:42
  • @ZdeslavVojkovic My program is able to execute around 200K requests per minute without this ``GetValues()`` line. When adding it, the speed goes down to around 10K requests per minute, which is a real big problem. – Elka Nov 18 '19 at 10:44
  • Are you also reading the body of the request in the code? – Zdeslav Vojkovic Nov 18 '19 at 10:47
  • @ZdeslavVojkovic No, I'm not. The code in my question is accurate – Elka Nov 18 '19 at 10:53
  • I've done some tests, and it seems that what causes the speed to decrease that much is the fact GetValues() iterates through the collection ... – Elka Nov 18 '19 at 11:00

0 Answers0