7

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);
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Karol Pisarzewski
  • 680
  • 3
  • 12
  • 21
  • 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 Apr 14 '18 at 06:48
  • 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 Apr 14 '18 at 06:53
  • I'd *expected* that an `Accept-Charset: utf-8` header would sort this out, but it didn't. Hmm. – Jon Skeet Apr 14 '18 at 07:46
  • Possible duplicate of https://stackoverflow.com/questions/37870084/net-core-doesnt-know-about-windows-1252-how-to-fix – ZiggZagg Apr 14 '18 at 08:02

2 Answers2

8

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);
ketura
  • 399
  • 1
  • 6
  • 11
7

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();
}
Gustav
  • 129
  • 4