.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 CORE中Encoding对GB2312等编码的支持的更多相关文章
- .NET Core中如何对Url进行编码和解码
我们在.NET Core项目中,可以用WebUtility类对Url进行编码和解码,首先我们要确保项目中引入了nuget包:System.Runtime.Extensions 当然这个nuget包默认 ...
- ASP.NET CORE中控制器内return HTML 内容自动编码问题
以前ASP.NET MVC中在控制器中直接 return Content( "<h1>测试测试</h1>"); 在前台VIEW上就显示加粗的文字了,但是在A ...
- 【asp.net core 系列】14 .net core 中的IOC
0.前言 通过前面几篇,我们了解到了如何实现项目的基本架构:数据源.路由设置.加密以及身份验证.那么在实现的时候,我们还会遇到这样的一个问题:当我们业务类和数据源越来越多的时候,我们无法通过普通的构造 ...
- .NET Core中使用GB2312编码
原文:.NET Core中使用GB2312编码 .NET Core默认不支持GB2312,如果直接使用Encoding.GetEncoding("GB2312")的时候会抛出异常. ...
- .NET Core 中使用GB2312编码报错的问题
错误描述 环境 dotnet 2.1.4 现象 当代码中使用 System.Text.Encoding.GetEncoding("GB2312") //或者 System.Text ...
- Unity3d发布的iOS产品中使用GB2312字符编码(CP936)
最近在开发中要用到GB2312字符编码(CP936),在C#代码中便有了如此代码 System.Text.Encoding.GetEncoding() 这在Unity3d 编辑器下运行没有任何问题,打 ...
- C#中获得机器的字符编码webName信息
在一次解决编码的问题过程中,发现Encoding.GetEncoding("GB2312") == Encoding.GetEncoding("GBK"),到底 ...
- ASP.NET Core中的缓存[1]:如何在一个ASP.NET Core应用中使用缓存
.NET Core针对缓存提供了很好的支持 ,我们不仅可以选择将数据缓存在应用进程自身的内存中,还可以采用分布式的形式将缓存数据存储在一个“中心数据库”中.对于分布式缓存,.NET Core提供了针对 ...
- 在 ASP.NET Core 中发送邮件遇到的坑_学习笔记
功能需求 因为项目需要有个忘记密码验证邮箱再重新修改密码的功能,然后我选用了很简单的一个方案,通过验证登录用户的邮箱然后发送邮件,通过这个邮件发送的链接地址来最后实现密码修改的小功能. 项目环境及实现 ...
随机推荐
- day 1 预习
day1 显卡:全称是显示接口卡,又称显示适配器,是电脑进行数模信号转换设备,承担输出显示图形的任务,显卡接在电脑主板上它将电脑的数字信号转换成模拟信号,让显示器显示出来.同时显卡还有图像处理能力,可 ...
- UMP系统架构 Mnesia
- 锋利的Jquery(Table,Checkbox)
1.Table奇数偶数行 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: ...
- python3 selenium 超时停止加载,并且捕捉异常, 进行下一步【亲测有效】
from selenium import webdriver import os import re class GetPage: def __init__(self, url_path): self ...
- JavaWeb开发购物车设计总结
一. 实体类设计 图书实体类 public class Book { private String id; private String name; private String author; pr ...
- leetcode-80-删除排序数组中的重复项②
题目描述: 第一次提交: class Solution: def removeDuplicates(self, nums: List[int]) -> int: nums.reverse() f ...
- H5在部分苹果手机IOS系统下重力感应无效
原因不明,反正在IOS系统12.2以上的普通的H5移动端网页的重力感应功能无反应 解决方法: 把链接改为HTTPS,经过测试,12.2系统以上的如果你的链接是HTTP的,重力感应在网页上是没调起的,改 ...
- 深入浅出Mybatis系列(二)---配置简介(mybatis源码篇)[转]
上篇文章<深入浅出Mybatis系列(一)---Mybatis入门>, 写了一个Demo简单体现了一下Mybatis的流程.本次,将简单介绍一下Mybatis的配置文件: 上次例子中,我们 ...
- tty who 命令
#tty : 查看当前终端对应的终端的设备文件 #who : 查看当前系统登录的所有用户及其信息
- 服务器IP配置功能实现小结
1. 服务器网卡配置文件 /etc/sysconfig/network/ifcfg-***(eth0) linux-f1s9:/etc/sysconfig/network # cat ifcfg-et ...