.NET CORE中Encoding对GB2312等编码的支持
最近.NET CORE做网络爬虫的时候,遇到了charset=gbk,转码的时候,发现直接使用Encoding.GetEncoding(“GB2312”)抛异常了。好吧,看到这个的时候,我是一脸懵逼的。无奈,毕竟.NET CORE不是.Net,做过精简,做过调整。然后,开始搜索相关资料,参考网址http://www.cnblogs.com/artech/p/encoding-registeration-4-net-core.html,做了两步操作:
1. 引入了System.Text.Encoding.CodePages.dll
2. 在启动的时候,注册EncodingProvider,执行代码如下:
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
这样,就可以在程序中使用GB3212编码了。
另外,我也在msdn上找到了相关资料,参考地址如下:
1. https://msdn.microsoft.com/en-us/library/ms404377(v=vs.110).aspx
相关内容:
By default, .NET Core does not make available any code page encodings other than code page 28591 and the Unicode encodings, such as UTF-8 and UTF-16. However, you can add the code page encodings found in standard Windows apps that target the .NET Framework to your app. For complete information, see the CodePagesEncodingProvider topic.
注解:默认情况下,.NET CORE只支持28951、UTF-8、UTF-16,其他的编码格式均不支持。但是,我们可以在.NET Framework开发的标准Windows应用程序中使用。完整的信息,请查看CodePagesEncodingProvider。CodePagesEncodingProvider的url就是如下2中的地址。
2. https://msdn.microsoft.com/en-us/library/mt643901(v=vs.110).aspx
相关内容:
The CodePagesEncodingProvider class extends EncodingProvider to make these code pages available to .NET Native and Universal Windows Platform (UWP) apps. To use these additional code pages, you do the following:
- Add a reference to the System.Text.Encoding.CodePages.dll assembly to your project.
- Retrieve a CodePagesEncodingProvider object from the static CodePagesEncodingProvider.Instance property.
- Pass the CodePagesEncodingProvider object to the Encoding.RegisterProvider method.
After an EncodingProvider object is registered, the encodings that it supports are available by calling the overloads of Encoding.GetEncoding; you should not call the EncodingProvider.GetEncoding overloads.
注解:CodePagesEncodingProvider类继承自EncodingProvider,可以使默认情况下不支持的编码,在.NET Native和UWP应用程序中进行使用。为了能使用这些编码,需要执行如下步骤:
1.添加the System.Text.Encoding.CodePages.dll到项目引用。(可以直接在nuget中下载安装)
2.把静态的CodePagesEncodingProvider.Instance 传递给Encoding.RegisterProvider方法,即执行代码:Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
备注:The CodePagesEncodingProvider class is supported in the .NET Native implementation of .NET Core only.
英文翻译的不到位,不过大致意思就是这样,给懒人一个偷懒的机会,高手们就自己体会原文的意境吧!
推荐一下自己的网站:www.mylofter.com:81,平行世界
最新文章
- .NET网页打印以及使用打印需要注意的事项(可能会引起VS崩溃的现象、打印预览后关闭功能不管用)
- 2015-2016-2 《Java程序设计》项目小组博客
- apache 日志为每个域名独立配置单独的日志文件
- html初学(一)
- Android基础总结(6)——内容提供器
- android 退出整个程序
- C# WinForm 中ComboBox数据绑定的问题 (转)
- IO流基础
- Heritrix源码分析(十五)
- 更有效率的使用 Visual Studio - 快捷键
- 使用Seam Framework + JBoss 5.0 开发第一个Web应用 - 简单投票程序
- BZOJ 3729: Gty的游戏 [伪ETT 博弈论]【学习笔记】
- sql server导出数据结构
- html 出现粒子线条,鼠标移动会以鼠标为中心吸附的特效之canvas-nest.js插件
- MySQL中间件之ProxySQL(8):SQL语句的重写规则
- python学习第7天
- Tensorflow 中的优化器解析
- Vue(五) 购物车案例
- MongoDB索引的种类与使用
- SSH Secure Shell Client--- the host may be dow