1

I am using HttpClient to send a post request to a remote server where I do not have control and trying to get the response as following

HttpResponseArgs result = //Make the request with HttpClient object ( I am skipping it here)

var stringResult = result?.Content.ReadAsStringAsync().Result; // exception thrown here as "UTF-8" is not supported encoding name

on debug I found the Content-Type header in response from remote server is set as "text/xml; charset ="UTF-8"" Please note the extra "" between UTF-8 this is causing the error if I remove the header from response and put a new content-Type header with "text/xml; charset =UTF-8". please note I removed the extra Quote around UTF-8 the code

result?.Content.ReadAsStringAsync().Result; // works fine now

Please suggest what can I do? I feel its a bug in .net framework as postman can interpret response of remote server in correct way.

the problem is in double quoate around UTF-8 in header of response

user3048027
  • 387
  • 1
  • 5
  • 24

4 Answers4

2

You can use custom EncodingProvider

public class Utf8EncodingProvider : EncodingProvider
{
    public override Encoding GetEncoding(string name)
    {
        return name == "\"UTF-8\"" ? Encoding.UTF8 : null;
    }

    public override Encoding GetEncoding(int codepage)
    {
        return null;
    }

    public static void Register()
    {
        Encoding.RegisterProvider(new Utf8EncodingProvider());
    }
}

Usage based on your question code:

Utf8EncodingProvider.Register(); //You should call it once at startup of your application
HttpResponseArgs result = //Make the request with HttpClient object ( I am skipping it here)

var stringResult = result?.Content.ReadAsStringAsync().Result; // Must be executed without exception.
Georgy Tarasov
  • 1,534
  • 9
  • 11
1

Perhaps force UTF-8 over the raw bytes, something like this

var buffer = await response.Content.ReadAsBufferAsync();
var byteArray = buffer.ToArray();
var responseString = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);
Yarin_007
  • 1,449
  • 1
  • 10
  • 17
0

There are many ASCII incompatible encodings in widespread use, particularly in Asian countries (which had to devise their own solutions before the rise of Unicode) and on platforms such as Windows, Java and the .NET CLR, where many APIs accept text as UTF-16 encoded data.

You could...

Pass it to python as a fileobject, use the encoding modules to change it back and forth. this technique has secured python as a "glue" language for C++ people.

Once again, just my opinion.

but i would strongly suggest you take Georgy Tarasovs answer

0

A modification of @Georgy Tarasov was necessary for me, because a page kept failing with utf8 as it sent a slight variation.

public class Utf8EncodingProvider : EncodingProvider
{
    private static readonly HashSet<string> Utf8Encoders = new HashSet<string>(
        new string[] { "utf-8", "utf8", "\"UTF-8\"" }
    );

    public override Encoding GetEncoding(string name)
    {
        if (Utf8Encoders.Contains(name))
        {
            return Encoding.UTF8;
        }

        return null;
    }

    public override Encoding GetEncoding(int codepage)
    {
        return null;
    }

    public static void Register()
    {
        Encoding.RegisterProvider(new Utf8EncodingProvider());
    }
}

Don't forget to call

Utf8EncodingProvider.Register();

One could convert name.toLower() for more hits

Samuel
  • 6,126
  • 35
  • 70