zoukankan      html  css  js  c++  java
  • 【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.Mapsig5.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);
      }

  • 相关阅读:
    比特币节点同步问题
    Vue用axios跨域访问数据
    vue之vue-cookies安装和使用说明
    vuejs目录结构启动项目安装nodejs命令,api配置信息思维导图版
    使用以太坊智能合约实现面向需要做凭证的企业服务帮助企业信息凭证区块链化
    将任意文件写入以太坊区块的方法,把重要事件,历史事件,人生轨迹加密记录到区块链永久封存
    Linux下几种重启Nginx的方式,找出nginx配置文件路径和测试配置文件是否正确
    php小数加减精度问题,比特币计算精度问题
    Fabric架构:抽象的逻辑架构与实际的运行时架构
    国外互联网大企业(flag)的涨薪方式
  • 原文地址:https://www.cnblogs.com/yffswyf/p/4826207.html
Copyright © 2011-2022 走看看