Search code examples
c#web-scrapinghttpclient

HttpClient GetStringAsync results are inconsistent


I have a very simple console application where the user can enter the name of a Twitch user to see if a Twitch profile with that name exists. Here is the code:

while (true)
{
    string username = Console.ReadLine();

    HttpClient client = new HttpClient();
    string result = await client.GetStringAsync($"https://www.twitch.tv/{username}");

    Console.WriteLine("User exists: " + !result.Contains("Twitch is the world"));

    Console.ReadKey();
}

The text "Twitch is the world" will only exists within the result if the provided url doesn't lead to an existing profile. The issue I'm having is that the HttpClient only seem to return the "right" result the first time around. For example, I start the program and enter "xqc". This will give me the result

User exists: True

However, if I now try to enter "xqc" again (or any other existing profile name for that matter) it will always result in the following result

User exists: False

I checked the actual string and the reason it's happening is because, for some reason, every request made after the first one will return Twitch's profile not found page. I don't see why this would happen. Is there something I'm missing or is there something else I should use here instead of HttpClient?


Solution

  • Console.ReadKey() is consuming the first character of your input, so the second time the string username will not contain "xqc", but "qc".