从HttpRequest收到的请求中含有中文字符,但是编码是unicode的,需要转换为GB2312才可以看到中文,否则是如%7C%B2%E2%CA%D4%09%09%之类的编码,请问该怎样转换??

解决方案 »

  1.   

    <META http-equiv="content-type" content="text/html; charset=gb2312">
      

  2.   

    自定义~public static string encodeDes(string des)
    {
    if ((des.IndexOf("%u")>0)||(des.IndexOf("?")<0))
    return des;
    else
    {
    string resultDes = "";
    string temp = "";
    resultDes = des.Substring(0,des.IndexOf("?")+1);  
             temp  =  des.Substring(des.IndexOf("?")+1);                 temp  =  temp+"&";
    while (temp.IndexOf("&")>0)
    {
    string firstDz = temp.Substring(0,temp.IndexOf("&"));     
    temp = temp.Substring(temp.IndexOf("&")+1);                if (firstDz.IndexOf("=")<0)
     continue;
                        resultDes += firstDz.Substring(0,firstDz.IndexOf("=")+1); 
                        resultDes += System.Web.HttpUtility.UrlEncodeUnicode(firstDz.Substring(firstDz.IndexOf("=")+1))+"&";                     
    }
    resultDes  = resultDes.Substring(0,resultDes.Length-1);
    return resultDes;
    }
    }
      

  3.   

    baobei7758(陵少):
    源字符为%7C%B2%E2%CA%D4%09%09%09%09%09&submit=%CC%E1%BD%BB
    用了你的方法,返回:
    %7C+%B2%E2%CA%D4%B2%E2%CA%D4%BF%C9%09%09%09%09%09&submit=%CC%
    这是为什么??
      

  4.   

    Response.Write(Server.UrlDecode(xxx));
      

  5.   

    其实 web.config中就有配置 <globalization requestEncoding="gb2312" responseEncoding="gb2312" />
       <!--<pages validateRequest="false"/>-->
    <httpRuntime 
    maxRequestLength="20000"
    />
      

  6.   

    找到了一个办法:
    Stream stream=Request.InputStream;
    StreamReader sr=new StreamReader(stream,Encoding.UTF8);
    string ss=sr.ReadToEnd();

    byte[] bs=Encoding.UTF8.GetBytes(ss);
    string cc=HttpUtility.UrlDecode(bs,0,bs.Length,Encoding.GetEncoding("GB2312"));