• 自动识别判断url中的中文参数是GB2312还是Utf8编码


    先看下面两个Url,他们传递的参数一样么?
    aaa.aspx?tag=.net%bc%bc%ca%f5
    aaa.aspx?tag=.net%e6%8a%80%e6%9c%af

    看起来好像是不一样,其实他们都是对".net技术"进行了UrlEncode,不过一个是GB2312的编码,一个是Utf-8的编码。
    Response.Write(Request.QueryString["tag"]); 
    可以得到Utf-8传过来的正确参数,而GB2312是错误的
    可以用下面的得到GB2312的URL

    string q = Request.Url.Query;
    System.Collections.Specialized.NameValueCollection nv = 
    System.Web.HttpUtility.ParseQueryString(q, System.Text.Encoding.GetEncoding("GB2312"));
    Response.Write(nv["Tag"]);

    [c-sharp] view plaincopy
      1. using System;  
      2. using System.Collections.Generic;  
      3. using System.Linq;  
      4. using System.Text;  
      5. using System.Web;  
      6. using System.Text.RegularExpressions;  
      7.   
      8. namespace ConsoleApplication2 {  
      9.     class Program {  
      10.         public static string DecodeURL2(String uriString) {  
      11.             if (Regex.IsMatch(  
      12.                 HttpUtility.UrlDecode(uriString, Encoding.GetEncoding("iso-8859-1")),  
      13.                 @"^(?:[/x00-/x7f]|[/xe0-/xef][/x80-/xbf]{2})+$" // 如果不考虑哪些什么拉丁文啊,希腊文啊。。。乱七八糟的外文,用这个短的正则  
      14.             )) {  
      15.                 return HttpUtility.UrlDecode(uriString, Encoding.GetEncoding("UTF-8"));  
      16.             } else {  
      17.                 return HttpUtility.UrlDecode(uriString, Encoding.GetEncoding("GB2312"));  
      18.             }  
      19.         }  
      20.   
      21.         public static string DecodeURL(String uriString) {  
      22.             if (Regex.IsMatch(  
      23.                 HttpUtility.UrlDecode(uriString, Encoding.GetEncoding("iso-8859-1")),  
      24.                 @"^(?:[/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])+$"  
      25.             )) {  
      26.                 return HttpUtility.UrlDecode(uriString, Encoding.GetEncoding("UTF-8"));  
      27.             } else {  
      28.                 return HttpUtility.UrlDecode(uriString, Encoding.GetEncoding("GB2312"));  
      29.             }  
      30.         }  
      31.   
      32.         public static void Main(string[] args) {  
      33.             Console.WriteLine("----------------------------------------------");  
      34.             Console.WriteLine(DecodeURL(".net%bc%bc%ca%f5"));  
      35.             Console.WriteLine(DecodeURL(".net%e6%8a%80%e6%9c%af"));  
      36.   
      37.   
      38.             Console.WriteLine("----------------------------------------------");  
      39.             Console.WriteLine(DecodeURL("%B8%A7%CB%B3%C7%E0%CB%C9%D2%A9%D2%B5"));  
      40.             Console.WriteLine(DecodeURL("%E6%8A%9A%E9%A1%BA%E9%9D%92%E6%9D%BE%E8%8D%AF%E4%B8%9A"));  
      41.   
      42.   
      43.             Console.WriteLine("------------------↓↓↓下面的出问题↓↓↓------------------");  
      44.   
      45.   
      46.             Console.WriteLine(DecodeURL("%E8%81%94%E9%80%9A")); // 正常  
      47.             Console.WriteLine(DecodeURL("%C1%AA%CD%A8")); // 发生编码误认  
      48.             // 编码误认,并没有好的解决方案,因为utf-8和gbk编码结果存在交叉,  我们都知道,记事本也都会出现这种情况  
      49.   
      50.             Console.WriteLine("------------------↑↑↑上面的出问题↑↑↑------------------");  
      51.   
      52.   
      53.             Console.WriteLine(DecodeURL2("%E8%81%94%E9%80%9A")); // 正常  
      54.             Console.WriteLine(DecodeURL2("%C1%AA%CD%A8")); // 不会误认  
      55.             Console.WriteLine("----------------------------------------------");  
      56.   
      57.   
      58.             Console.ReadKey();  
      59.         }  
      60.     }  

  • 相关阅读:
    人月神话阅读笔记01
    HTML中常用meta整理
    前端开发中的SEO
    webGL学习笔记
    webGL 学习教程
    HTMl5的sessionStorage和localStorage
    Validform使用
    gulp详细入门教程
    gulp.spriteSmith使用
    gulp.spritesmith修改px为rem单位
  • 原文地址:https://www.cnblogs.com/zxktxj/p/2758374.html
Copyright © 2020-2023  润新知