.NET Core中使用编码GB2312报错‘GB2312‘ is not a supported encoding name 解决方案


问题描述

今天在.Net Core中对外网新闻进行爬虫抓取,最初抓取到的新闻中出现了乱码,后来通过GB2312进行了编码,结果报错“ Unhandled Exception: System.ArgumentException: 'GB2312' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.”

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

————————————————

解决方案

1. 项目添加引用

在NuGet包中安装包System.Text.Encoding.CodePages

System.Text.Encoding.CodePages

2. 对编码进行注册

在使用编码方法(Encoding.GetEncoding("GB2312"))之前,对编码进行注册( Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);),代码如下 

在 startUp.cs 类 ConfigureServices 方法中对编码进行注册

#region 配置GB2312编码
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
#endregion

3. 使用

var encodGB2312 = Encoding.GetEncoding("GB2312");
byte[] byte03 = encodGB2312.GetBytes("04033屏幕测试");

参考: https://blog.csdn.net/liuzhenhe1988/article/details/109091568

 

 

版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
YES开发框架
上一篇:Tinymce编辑器加载后默认为P元素,修改为DIV
下一篇:C# 邮件发送,阿里云邮箱参数设置,邮件发送测试工具下载
评论列表

发表评论

评论内容
昵称:
关联文章

.NET Core使用编码GB2312GB2312is not a supported encoding name 解决方案
Python读取excel xlrd读取xlsx:Excel xlsx file; not supported
Visual Studio (VS) 使用Gulp ReferenceError: primordials is not defined
.Net启动程序:It was not possible to find any compatible framework version
.net Core Swagger the same schemaid is already used for type swagger
.NET Core Winform 打开窗体设计器
WCF常见的:The content type text
.NETCore 运行项目:Could not load file or assembly ***
VUE使用sass提示在sass文件提示Syntax Error : this.getOptions is not a function
Python多线程试用wmi
EF MySQL取值时出错: Specified cast is not valid
Python使用html解析包Requests-html运行py文件没问题,Pyinstaller打包exe后运行
.net core MVC页面源码文件文被编码
支付宝获取用户信息时,提示 对不起,访问出了 E004:AUTH_REDIRECT_URL_NOT_IN_WHITE
C#URL编码
IIS服务器访问woff2字体文件 net::ERR_ABORTED 404 (Not Found)
asp.net core MVC路由添加.html伪静态url时报
插入代码页面
ASP.NET Core MVC 在过滤器ActionFilter保存页面的生成的html静态页面文件
C#代码编码规范手册 软件开发规范 开发指南

联系我们
联系电话:15090125178(微信同号)
电子邮箱:garson_zhang@163.com
站长微信二维码
微信二维码