解决Cookie乱码

在Asp.net的HttpCookie中写入汉字,读取值为什么全是乱码?其实这是因 为文字编码而造成的,汉字是两个编码,所以才会搞出这么个乱码出来!其实解决的方法很简单:只要在写入Cookie时,先将其用Url编码,然后再写入, 当我们读取时再解码就OK了,希望更多的***妹能解决它,呵呵!
   例子:
   Cookie的写入:
1HttpCookie cookie=new HttpCookie("Simple");
2cookie.Values.Add("Simple1",HttpUtility.UrlEncode("大叔,你好!"));
3cookie.Values.Add("Simple2","English is OK!Nothing we should do!");
4Response.AppendCookie(cookie); Cookie的读取:
1HttpCookie cookie=Request.Cookies["Simple"];
2string simple1=HttpUtility.UrlDecode(cookie["Simple1"]);
3string simple2=cookie["Simple2"]; 这样simple1="大叔,你好!";simple2="English is OK!Nothing we should do!";

 

 

 

 

 

下面是没用过的资料

 

设置cookie时:

HttpCookie cookie = Request.Cookies["UserName"];
                if (Session["UserName"] != null)
                {
                    string userName = Server.UrlEncode(Session["UserName"].ToString());
                    if (cookie != null)
                    {
                        cookie.Value = userName;
                        //cookie.Expires = DateTime.Now.AddDays(1);
                        cookie.Domain = BasePage.DomainName;
                        Response.Cookies.Set(cookie);
                    }
                    else
                    {
                        cookie = new HttpCookie("UserName");
                        cookie.Value = userName;
                        //cookie.Expires = DateTime.Now.AddDays(1);
                        cookie.Domain = BasePage.DomainName;
                        Response.Cookies.Add(cookie);
                    }
                }


获取cookie时:

private string _loginUserName;
        public string LoginUserName
        {
            get
            {
                if (Session["UserName"] != null && Session["UserName"].ToString() != "")
                {
                    _loginUserName = Session["UserName"].ToString();
                }
                else if (Request.Cookies["UserName"] != null && Request.Cookies["UserName"].Value != "")
                {
                    _loginUserName = Server.UrlDecode(Request.Cookies["UserName"].Value);
                }
                else
                {
                    _loginUserName = null;
                }
                return _loginUserName;
            }
        }

 

 

下面是写入cookie的代码

[csharp] view plain copy
  1. HttpCookie cookie = new HttpCookie("username");  
  2.             cookie.Value = "张三,14,images/1.jpg";  
  3.             cookie.Expires = DateTime.Now.AddDays(1);  
  4.             Response.Cookies.Add(cookie);  


 

下面是读取cookie的代码

[csharp] view plain copy
  1. if (Request.Cookies["username"]!=null)  
  2.             {  
  3.                 string username = Request.Cookies["username"].Value;  
  4.                 Response.Write(username);  
  5.             }  


 

有时读取出来的cookie值中的中文部分可能是乱码,不管是有什么导致的,我们都可以通过编码进行解决

更改上面写入cookie的代码

[csharp] view plain copy
  1. HttpCookie cookie = new HttpCookie("username");  
  2.             cookie.Value = HttpUtility.UrlEncode("张三,14,images/1.jpg",Encoding.GetEncoding("UTF-8"));  
  3.             cookie.Expires = DateTime.Now.AddDays(1);  
  4.             Response.Cookies.Add(cookie);  


 

更改上面读取cookie的代码

[csharp] view plain copy
    1. if (Request.Cookies["username"]!=null)  
    2.            {  
    3.                string username =HttpUtility.UrlDecode(Request.Cookies["username"].Value,Encoding.GetEncoding("UTF-8"));  
    4.                Response.Write(username);  
    5.            } 
posted @ 2016-05-31 17:09  C#winform软件设计  阅读(280)  评论(0编辑  收藏  举报