private string Gb2312ToUtf8(string str)
{
    byte[] getBt = Encoding.GetEncoding("GB2312").GetBytes(str);   
    return Encoding.GetEncoding("utf-8").GetString(getBt);
}页面传的值是汉字。
如:http://www.abc.com/空调.html,项目是采用gb2312编码的(不能改,会影响整个项目),实际地址是:http://www.abc.com/Product.aspx?keywords=空调。我把传过来的汉字转换成utf-8编码形式,似乎没有问题。
但是当我传奇数个汉字的时候,问题就出现了,发现少了一个字。
如:http://www.abc.com/空调机.html 获得的值就是空调,偶数个汉字没有问题,能正常获取。这是什么问题?有没有其它的gb2312转utf-8的方法?

解决方案 »

  1.   

    private string Gb2312ToUtf8(string str)  // 实际上 C# 中字符创永远是按 Unicode 表示的
    {
    byte[] getBt = Encoding.GetEncoding("GB2312").GetBytes(str);  //  这里的字节编码是按 GB2312
    return Encoding.GetEncoding("utf-8").GetString(getBt); // GB2312 >> utf-8 ????
    }不知道 lZ 你这么样处理有什么效果?假如,你要转换,应该是,得到 GB2312 的字节数组,然后转换成 System.String ,再转换成 utf-8 的字节数组,这样才有意义,否则 对应 System.String 你转来专区,它还是 Unicode
      

  2.   

    try:    private string Gb2312ToUtf8(string str)
            {
                byte[] getBt = Encoding.GetEncoding("GB2312").GetBytes(str);
                getBt = System.Text.Encoding.Convert(Encoding.GetEncoding("GB2312"), Encoding.UTF8, getBt);
                return Encoding.GetEncoding("utf-8").GetString(getBt);
            }
      

  3.   

    如果你是用Request.Url来取得页面地址就不对了.
      

  4.   

    我取页面地址是用 Request.RawUrl
    取关键词是用 Request["keywords"].ToString().Trim()
      

  5.   

    if( Request["keywords"] != null )
    {
      if( Request.RawUrl.IndexOf("keywords=") != -1 )
      {
         key = Request["keywords"].ToString().Trim();
      }
      else
      {
        key = Gb2312ToUtf8(Request["keywords"].ToString().Trim());
      }
    }
      

  6.   

    参考这个protected string GetQueryString(string sKey,System.Text.Encoding e)
            {
                string QueryString = Server.UrlDecode(System.Web.HttpUtility.UrlDecode(Request.ServerVariables["QUERY_STRING"], e));
                System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(sKey + "=([^&$]*?)(&|$)");
                System.Text.RegularExpressions.Match m = reg.Match(QueryString);
                if (m.Success)
                {
                    return m.Result("$1");
                }
                else return String.Empty;
            }
    //以上这个不受编码影响,只需知道原来传入的编码就可.
      

  7.   

    key = GetQueryString(Request["keywords"].ToString().Trim(),Encoding.GetEncoding("GB2312"));  ??????
      

  8.   

    就是因为为了避免使用Request.QueryString所以才写了个函数,应该这样key = GetQueryString("keywords",Encoding.GetEncoding("GB2312"));
      

  9.   

    http://www.abc.com/空调.html  取到的值是乱码
    http://www.abc.com/Product.aspx?keywords=空调 取值是正确的
      

  10.   

    http://www.abc.com/空调.html  取到的值是乱码
    ==>你是怎么取的?
      

  11.   

    GetQueryString("keywords",Encoding.GetEncoding("GB2312"));
      

  12.   

    protected string GetFileName(System.Text.Encoding e)
            {
                string QueryString = Server.UrlDecode(System.Web.HttpUtility.UrlDecode(Request.ServerVariables["PATH_INFO"], e));
                System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"/([^\./]*?)\.aspx");
                System.Text.RegularExpressions.Match m = reg.Match(QueryString);
                if (m.Success)
                {
                    return m.Result("$1");
                }
                else return String.Empty;
            }
      

  13.   

    感谢 慕白兄
    用你的GetQueryString方法,可以取到正确的值了。if( Request["keywords"] != null )
    {
      if( Request.RawUrl.IndexOf("keywords=") != -1 )
      {
         key = Request["keywords"].ToString().Trim();
      }
      else
      {
        key = GetQueryString("keywords",Encoding.GetEncoding("utf-8"));
      }
    }
    不过出现了个新问题:分页的时候乱码了http://www.abc.com/Product.aspx?keywords=鍘嬬缉鏈&page=2,我用的吴旗娃的分页控件,采用存储过程分页方式。