Runtime: System.Net.Http.HttpContent.ReadAsStringAsync fails for ISO-8859-15 (Latin-9)

Created on 7 Oct 2017  路  4Comments  路  Source: dotnet/runtime

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?

area-System.Net.Http question

Most helpful comment

Try adding Encoding.RegisterProvider(CodePagesEncodingProvider.Instance) to your Main method. It will register larger set of encodings.

All 4 comments

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:

  • Run dotnet.exe add package System.Text.Encoding.CodePages
  • Add using 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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

omajid picture omajid  路  3Comments

iCodeWebApps picture iCodeWebApps  路  3Comments

v0l picture v0l  路  3Comments

btecu picture btecu  路  3Comments

chunseoklee picture chunseoklee  路  3Comments