Home > Enterprise >  HttpClient.GetAsync returns weird result for some websites
HttpClient.GetAsync returns weird result for some websites

Time:10-10

Please consider this simple Code:

private async void button3_Click(object sender, EventArgs e)
{
    textBox1.Text = "It begins...";
    await DoAsync_Button3();
}

private async Task DoAsync_Button3()
{
    HttpClient client = new HttpClient();
    HttpResponseMessage response = await client.GetAsync("https://www.stackoverflow.com/");
    response.EnsureSuccessStatusCode();
    string responseBody = await response.Content.ReadAsStringAsync();
    textBox1.Text = responseBody;
}

and the simple UI:

enter image description here

the problem is for some websites I got some weird result. for example: enter image description here

enter image description here

enter image description here

I got weird result whereas StatusCode is 200. How do I determine where the problem is? Thanks

CodePudding user response:

You need to decompress the response:

HttpClient httpClient = new HttpClient(new HttpClientHandler
{
    AutomaticDecompression = System.Net.DecompressionMethods.All
});
string responseAsString = await httpClient.GetStringAsync("https://www.dotnetperls.com/");
Console.WriteLine(responseAsString);

If you are using .NET 5 or 6, you can use SocketsHttpHandler instead of HttpClientHandler.

  • Related