收到的回复如下:
'UTF8' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.
Parameter name: name
The character set provided in ContentType is invalid. Cannot read content as string using an invalid character set.
答案 0 :(得分:1)
我认为问题是服务器在标头中返回错误的字符集名称。而是charset=utf-8
设置charset=utf8
。
在反序列化方面,HttpClient
(RestEase使用它)无法理解要使用的编码。
要解决此问题,您需要添加一个指向通常的UTF8编码的新提供程序。
您应该在" global"添加代码级别(例如Main
方法):
EncodingProvider provider = new CustomUtf8EncodingProvider();
Encoding.RegisterProvider(provider);
并且类,女巫为错误的字符集名称返回UTF8编码:
public class CustomUtf8EncodingProvider : EncodingProvider
{
public override Encoding GetEncoding(string name)
{
return name == "utf8" ? Encoding.UTF8 : null;
}
public override Encoding GetEncoding(int codepage)
{
return null;
}
}
我这样检查
EncodingProvider provider = new CustomUtf8EncodingProvider();
Encoding.RegisterProvider(provider); //If comment this row - you get exception "'UTF8' is not a supported encoding name...", if uncomment - it works good
Console.WriteLine(Encoding.GetEncoding("utf8").GetString(new byte[]{0x31,0x32,0x33}));
注意强>
仅适用于.net 4.6或更高版本