首页 > Web开发 > 详细

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

时间:2021-09-17 13:31:52      阅读:33      评论:0      收藏:0      [点我收藏+]

最近在用.net core写一个爬虫抓取网页上的数据,碰到了网页编码是GBK的页面,抓取的数据都是乱码,当使用Encoding.GetEncoding(“GBK”)的时候抛出了异常:

‘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

当改用GB2312的时候也抛出了同样的异常:

‘GB2312‘ is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.
Parameter name: name

从异常上来看是不支持GB2312和GBK。而且提到了需要注册EncodingProvider的方法。CodePagesEncodingProvider定义在NuGet包“System.Text.Encoding.CodePages”之中。所以我们就需要添加System.Text.Encoding.CodePages的依赖。然后在Project.json中添加

{
"dependencies": { "System.Text.Encoding.CodePages": "4.0.1-rc2-24027" }
}

在代码中还需要添加Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);如图:

技术分享图片

再次启动就可以可以使用GB2312

 

转载:https://www.cnblogs.com/liemei/p/7884172.html

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

原文:https://www.cnblogs.com/wugh8726254/p/15302391.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!