【UWP】解析GB2312、GBK编码网页乱码问题
在WebHttpRequest请求网页后,获取到的中文是乱码,类似这样:
<title>˹ŵ��Ϸ���������� - ��̳������ - ˹ŵ��Ϸ����</title>
原因是网页多种编码方式(上述charset=gbk),UWP中Encoding能够支持UTF-8、Unicode,但是不支持gb2312、gbk等编码。
因此我们需要在获取流的时候对编码方式进行处理。
var reader = new StreamReader(stream, Encoding.GetEncoding("UTF-8")); //UTF-8编码
网上搜到WP8下的解决方法,但是不能够直接用于UWP中,需要修改:
http://encoding4silverlight.codeplex.com/ 下载
找到DBCD下的三个文件,将他们添加到解决方案中
1.DBCSEncoding.cs 2.Maps\big5.bin 3.gb2312.bin
【原方案:将两个.bin文件设置为嵌入的资源,将DBCSEncoding.cs设置为编译】
但你会发现:这条代码报错,意思是获取.bin文件的流:
Stream stream = typeof(DBCSEncoding).Assembly.GetManifestResourceStream(typeof(DBCSEncoding).Assembly.GetManifestResourceNames().Single(s => s.EndsWith("." + name + ".bin")))
是因为Type类型找不到Assembly属性;
于是想到用Application.GetResourceStream(uri)的方式取得.bin文件,但是UWP中没有了这个API了。
又查到如下:
http://msdn.microsoft.com/en-us/library/windows/apps/xaml/br229571.aspx 其中的Application object and app model 的最后一点:
Silverlight apps could either package application parts into the deployment package, as external parts, or download components on demand. A Metro style app has these choices too, but the APIs used to access the package parts are different. Where Silverlight uses Application.GetResourceStream, a Metro style app uses a more generalized model, where the installed package is just a storage folder. For example, you can call Package.InstalledLocation and then call a variety of StorageFolder APIs (most of which are async) in order to get any other packaged components.
意思是说Application.GetResourceStream的对应方式是通过Package.InstalledLocation 拿到安装目录,然后通过文件操作去读取资源。
所以就来看看如何操作:我把.bin放在另一个叫做DataHelperLib的Maps文件夹里。
当然你需要吧.bin的生成操作设置为内容,安装包里面才会出现
public async static Task<Stream> GetInstall()
{
//此处只是简单的获取到gb2312.bin文件
var folderInstall = Windows.ApplicationModel.Package.Current.InstalledLocation; //获取安装包的位置
var folder = await folderInstall.GetFolderAsync("DataHelperLib"); //获取DataHelperLib文件夹
var mapFolder = await folder.GetFolderAsync("Maps"); //获取Maps文件夹
var file = await mapFolder.GetFileAsync("gb2312.bin"); //获取gb2312.bin
Stream stream = await file.OpenStreamForReadAsync(); //获取文件流
return stream;
}
之后将DBCSEncoding.cs错误的那条代码替换成
using(Stream stream = await StorageHelper.GetInstall()); //别忘了将方法的返回值改为Task<DBCSEncoding>
最后以这种编码方式读取流:
using (var stream = response.GetResponseStream())
{
var reader = new StreamReader(stream, await DBCSEncoding.GetDBCSEncoding("GB2312"));
string content = reader.ReadToEnd();
//DoSomething with callback stream
OnSuccess(content, response.StatusCode);
}