ArgumentException: 'gb2312' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.

 

当前框架版本.Net 5.0

问题:.net core中使用GB2312编码的问题

ArgumentException: 'gb2312' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.

 

The character set provided in ContentType is invalid. Cannot read content as string using an invalid character set.”
ArgumentException:
'gb2312' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method. Arg_ParamName_Name

 

 

解决方案:

在调用请求时候,注册字符集:

//注册字符集
                System.Text.EncodingProvider provider = System.Text.CodePagesEncodingProvider.Instance;
                Encoding.RegisterProvider(provider);

 

 

使用案例:

复制代码
HttpClient client = new HttpClient();
using (MemoryStream ms = new MemoryStream())
{

    byte[] bytes = Encoding.UTF8.GetBytes(paramJson);
    ms.Write(bytes, 0, bytes.Length);

    HttpContent hc = new StreamContent(ms);
    hc.Headers.ContentType = new MediaTypeHeaderValue("text/html");
    hc.Headers.ContentEncoding.Add("utf-8");

    //注册字符集
    System.Text.EncodingProvider provider = System.Text.CodePagesEncodingProvider.Instance;
    Encoding.RegisterProvider(provider);

    HttpResponseMessage resp = await client.PostAsync(url, hc);

    string result = await resp.Content.ReadAsStringAsync();
    //判断结果处理
}
复制代码

 

更多:

.Net Core 发送https请求/.net core 调用数字证书 使用X509Certificate2

.Net Standard HttpClient封装Htt请求常用操作整理

.Net Standard 类库的创建和使用

posted @   天马3798  阅读(575)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
历史上的今天:
2016-05-13 Asp.Net Mvc MapRoute .html不起作用(转)
2014-05-13 从客户端检测到有潜在危险的Request.Form值
点击右上角即可分享
微信分享提示