zoukankan      html  css  js  c++  java
  • 如何自动识别判断url中的中文参数是GB2312还是Utf-8编码?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Text.RegularExpressions;

    namespace ConsoleApplication2 {
            class Program {
                    public static string DecodeURL2(String uriString) {
                            if (Regex.IsMatch(
                                    HttpUtility.UrlDecode(uriString, Encoding.GetEncoding("iso-8859-1")),
                                    @"^(?:[x00-x7f]|[xe0-xef][x80-xbf]{2})+$" // 如果不考虑哪些什么拉丁文啊,希腊文啊。。。乱七八糟的外文,用这个短的正则
                            )) {
                                    return HttpUtility.UrlDecode(uriString, Encoding.GetEncoding("UTF-8"));
                            } else {
                                    return HttpUtility.UrlDecode(uriString, Encoding.GetEncoding("GB2312"));
                            }
                    }

                    public static string DecodeURL(String uriString) {
                            if (Regex.IsMatch(
                                    HttpUtility.UrlDecode(uriString, Encoding.GetEncoding("iso-8859-1")),
                                    @"^(?:[x00-x7f]|[xfc-xff][x80-xbf]{5}|[xf8-xfb][x80-xbf]{4}|[xf0-xf7][x80-xbf]{3}|[xe0-xef][x80-xbf]{2}|[xc0-xdf][x80-xbf])+$"
                            )) {
                                    return HttpUtility.UrlDecode(uriString, Encoding.GetEncoding("UTF-8"));
                            } else {
                                    return HttpUtility.UrlDecode(uriString, Encoding.GetEncoding("GB2312"));
                            }
                    }

                    public static void Main(string[] args) {
                            Console.WriteLine("----------------------------------------------");
                            Console.WriteLine(DecodeURL(".net%bc%bc%ca%f5"));
                            Console.WriteLine(DecodeURL(".net%e6%8a%80%e6%9c%af"));


                            Console.WriteLine("----------------------------------------------");
                            Console.WriteLine(DecodeURL("%B8%A7%CB%B3%C7%E0%CB%C9%D2%A9%D2%B5"));
                            Console.WriteLine(DecodeURL("%E6%8A%9A%E9%A1%BA%E9%9D%92%E6%9D%BE%E8%8D%AF%E4%B8%9A"));


                            Console.WriteLine("------------------↓↓↓下面的出问题↓↓↓------------------");


                            Console.WriteLine(DecodeURL("%E8%81%94%E9%80%9A")); // 正常
                            Console.WriteLine(DecodeURL("%C1%AA%CD%A8")); // 发生编码误认
                            // 编码误认,并没有好的解决方案,因为utf-8和gbk编码结果存在交叉,  我们都知道,记事本也都会出现这种情况

                            Console.WriteLine("------------------↑↑↑上面的出问题↑↑↑------------------");


                            Console.WriteLine(DecodeURL2("%E8%81%94%E9%80%9A")); // 正常
                            Console.WriteLine(DecodeURL2("%C1%AA%CD%A8")); // 不会误认
                            Console.WriteLine("----------------------------------------------");


                            Console.ReadKey();
                    }
            }
    }
  • 相关阅读:
    [引]Windows窗体编程基础学习: 对话框组件
    UML学习四:UML在微软的VS与Visio间的应用 VEA(Visio for Enterprise Architects)
    SQL查询 FOR XML [RAW|AUTO|EXPLICIT]
    微软企业类库:Enterprise Library for .NET Framework 2.0 January 2006
    自定义制作 自动定时更换图片的桌面背景 html页面
    [引]Windows Server 2003 : Windows 群集
    LOVE
    SQL Server Mobile 学习(三):SQL Server Mobile 远程数据访问(RDA)
    游戏分类
    [摘]UML学习二:标准建模语言UML的静态建模机制
  • 原文地址:https://www.cnblogs.com/hzpin/p/3924687.html
Copyright © 2011-2022 走看看