zoukankan      html  css  js  c++  java
  • 解决html向aspx页面传值汉字乱码问题

     
    项目遇到了下面的问题:

    搜索引擎页面     首页为html, 页面有个站内搜索功能,通过提交表单方式传参到.aspx页面。起初不做任何操作,aspx搜索结果页面显示乱码。    原因:html采用的是Gb2312  而aspx页面默认是utf编码,显然会乱码。

    正常情况下  URL编码格式,如字:
    GB2312
    编码:BABA URL格式:%BA%BA
    UTF-
    8 编码:E6B189 URL格式:%E6%B1%89

    1.起初我是在Page_Load里这样操作:
          Encoding gb2312 = Encoding.GetEncoding("gb2312");
          Response.ContentEncoding = gb2312;
    aspx页面的编码格式改为:<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
     
    出现的情况是,页面上的汉字竟然都成了口口口。



    2.之后网上再找了些资料,通过这种方式转化编码。
            byte[] barr = System.Text.Encoding.UTF8.GetBytes(Key);
            Key = System.Text.Encoding.GetEncoding("gb2312").GetString(barr);
    结果有些汉字可以转化过来,但大多汉字会变成:“锟斤拷”    经典乱码。
    原因:
           Unicode和老编码体系的转化过程中,肯定有一些字,用Unicode是没法表示的,Unicode官方用了一个占位符来表示这些文字,这就是:U+FFFD REPLACEMENT CHARACTER
    那么U+FFFD的UTF-8编码出来,恰好是 '\xef\xbf\xbd'。
    如果这个'\xef\xbf\xbd',重复多次,例如 '\xef\xbf\xbd\xef\xbf\xbd',然后放到GBK/CP936/GB2312/GB18030的环境中显示的话
    一个汉字2个字节,最终的结果就是:锟斤拷 哈哈。。。
    此方法还是不行。

    3.无奈只能修改 web.config, 因为该文件的重要性,对其他文件也会有很大的影响,我一般很少去动,尽量在自己的页面完成,但这次看来是不行了。

    web.config
    文件 

     <globalization requestEncoding=
    "utf-8" responseEncoding="utf-8" fileEncoding="utf-8"/>

    改为

     <globalization requestEncoding=
    "gb2312" responseEncoding="gb2312" fileEncoding="utf-8"/>


    结果汉字传参过来正确编码,没了乱码。  经过全站一番测试,问题严重了,后台很多文件都乱码了。看来web.config文件还是不要去改,小题大作。

    4.最后通过解析地址栏编码,我接受参数的时候只要 
           System.Collections.Specialized.NameValueCollection nv = System.Web.HttpUtility.ParseQueryString(Request.Url.Query, System.Text.Encoding.GetEncoding("GB2312"));
      汉字就不会乱码了。
        可是要注意的是提交表单的方法一定要是Get方式,Post方式接收不到值。

    我解决问题的步骤如下,也包括不成功的步骤

    1.html页面:
        <form name="Search" action="Search.aspx"  method="get">
      <tr>
        <td height="41"><img src="img/Inf_-_01.gif" width="394" height="41" /></td>
        <td><img src="img/Inf_-_02.gif" width="50" height="41" /></td>
        <td background="img/Inf_-_03.gif" width="22"></td>
        <td width="140" align="left" valign="bottom" background="img/Inf_-_04.gif"><label>
       
        <span class="STYLE10">站内搜索:</span></label><input name="WordKey" type="text" id="WordKey" size="40"></td>
       
        <td width="71" valign="bottom" style="background:url(img/Inf_-_050.gif) repeat-x"><input type="image" style="71; height:21;" src="img/Inf__05.gif" onclick="Search();"></td>
        <td><img src="img/Inf_-_06.gif" width="50" height="41" /></td>
        <td width="127" align="center" valign="bottom" style="background:url(img/Inf_-_070.gif) repeat-x"><span class="STYLE10"><script type="text/javascript">writeDateInfo();</script></span></td>
      </tr>
      </form>


    2. aspx页面引入命名空间

    using System.Text;
    using System.Collections.Specialized;

        string CurrentStr = Request.Url.Query;
                 System.Collections.Specialized.NameValueCollection nv = System.Web.HttpUtility.ParseQueryString(CurrentStr, System.Text.Encoding.GetEncoding("GB2312"));
                 string Key = nv["WordKey"];

     3.搞定。哈哈

  • 相关阅读:
    Find the Smallest K Elements in an Array
    Count of Smaller Number
    Number of Inversion Couple
    Delete False Elements
    Sort Array
    Tree Diameter
    Segment Tree Implementation
    Java Programming Mock Tests
    zz Morris Traversal方法遍历二叉树(非递归,不用栈,O(1)空间)
    Algorithm about SubArrays & SubStrings
  • 原文地址:https://www.cnblogs.com/lhws/p/2465062.html
Copyright © 2011-2022 走看看