5
votes

I wrote some simple code with async await, but when I try to run it, the compiler throws a System.InvalidOperationException.

The full error message is:

Unhandled Exception: System.InvalidOperationException: The character set provided in ContentType is invalid. Cannot read content as string using an invalid character set.

System.ArgumentException: 'ISO-8859-2' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.

The code:

class Program
{
    static async Task Main(string[] args) => await asyncDoItAsync();

    private static async Task<string> asyncDoItAsync()
    {
        HttpClient client = new HttpClient(); 
        Task<string> url = client.GetStringAsync("http://google.pl");

        while(!url.IsCompleted)
        {
            WhenWaiting();
        }

        string url_length  = await url;

        return url_length;
    }

    private static void WhenWaiting()
    {
        Console.WriteLine("Waiting ...");
        Thread.Sleep(90);
    }
}
2
I'm looking at the actual problem and hope to have an answer for you soon, but just as an aside, it's not the compiler throwing the exception. Apart from cases using dynamic typing (where part of the compiler is effectively present at execution time), any compiler errors would be present when you compile, not when you run. - Jon Skeet
As a secondary aside, it's great that you've provided a sample, but it can be simplified - I've got a more minimal example that still demonstrates the problem... would you be happy for me to edit it into your question? - Jon Skeet
I'd expected that an Accept-Charset: utf-8 header would sort this out, but it didn't. Hmm. - Jon Skeet

2 Answers

5
votes

GetStringAsync does not seem to support iso-8859 in the response, and it's basically nothing you can do about it. So you need to start with GetAsync instead. Be sure to clear your existing headers or it probably will still fail. Below works for me for the url you provided (although I'm using ISO-8859-1 and not 2):

var client = new HttpClient();
client.DefaultRequestHeaders.Clear();
string s = null;
var result = await client.GetAsync("http://google.pl");
using (var sr = new StreamReader(await result.Content.ReadAsStreamAsync(), Encoding.GetEncoding("iso-8859-1")))
{
     s = sr.ReadToEnd();
}
0
votes

In .NET Core not all code pages are available by default. See this answer here for details on how to enable more.

Basically, install the System.Text.Encoding and System.Text.Encoding.CodePages packages from Nuget, and then include this line before running any http requests:

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);