I'm trying to read the HTTP response from a web service, where the content type header is:
Content-Type: text/html; charset=ISO-8859-15
When invoking System.Net.Http.HttpContent.ReadAsStringAsync() my app fails with System.InvalidOperationException "The character set provided in ContentType is invalid. Cannot read content as string using an invalid character set."
ISO-8859-15 (Latin-9) though is a valid charset, and the problem seems to be in the incomplete list of charsets hardcoded in .NET (System.Globalization I think).
Is this a bug? Is there another way to read content encoded with this (or any other) charset, other than going lower level into the data stream?
Try adding Encoding.RegisterProvider(CodePagesEncodingProvider.Instance) to your Main method. It will register larger set of encodings.
@jkotas is there a corresponding class or solution in .NET Core?
It is the solution for .NET Core. To apply it:
dotnet.exe add package System.Text.Encoding.CodePagesusing System.Text; and Encoding.RegisterProvider(CodePagesEncodingProvider.Instance)I assume @jkotas's answer was sufficient. Closing.
Feel free to reopen if it is not the case.
Most helpful comment
Try adding
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance)to your Main method. It will register larger set of encodings.