在.net core 中使用了HttpClient库进行网络访问,有些网站成功,有些网络失败。报错内容如下:
The character set provided in ContentType is invalid. Cannot read content as string using an invalid character set.
‘gbk’ is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.Parameter name: name
“gbk”字符集不被支持,需要 Encoding.RegisterProvider 进行注册。需要下载包
System.Text.Encoding.CodePages来进行处理。使用如下:
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
使用以上代码进行注册后错误就不再出现。.net core处理还真的有少许麻烦。
转载请注明:清风亦平凡 » HttpClient在.net Core 中报字符集报错的问题