Search code examples
c#httpclient

HttpClient can't parse "UTF-8" Content-Type


I am experiencing a known bug in the HttpClient. Anytime the server response contains "UTF-8" (including quotes), an exception is triggered:

The character set provided in ContentType is invalid. Cannot read content as string using an invalid character set. ---> System.ArgumentException: '"utf-8"' is not a supported encoding name. 

Example code:

HttpClient _client = new HttpClient();
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, "https://www.facebook.com");
requestMessage.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.4044.55 Safari/537.36");

HttpResponseMessage response = _client.SendAsync(requestMessage).GetAwaiter().GetResult();

What is the usual workaroud? I am using .NETFramework 4.6.1.


Solution

  • To workaround the referenced issue:

    using (var client = new HttpClient())
    {
        HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, 
            "https://www.facebook.com");
        HttpResponseMessage response = await client.SendAsync(requestMessage);
    
        byte[] buf = await response.Content.ReadAsByteArrayAsync();
        string content = Encoding.UTF8.GetString(buf);
    }