标签:
在设置cookie时,如果存入cookie中的内容为中文,则读取后变成乱码,英文或者数字不存在这种问题。解决办法是不直接存取,而是采用编码的方法进行存取。
1、设置cookie
public void setCookies()
{
HttpCookie ck = new HttpCookie("ckLogin");
ck.Values.Add("cID", HttpUtility.UrlEncode(UserID));
ck.Values.Add("cPWD", HttpUtility.UrlEncode(UserPWD));
ck.Values.Add("cAutoLogin", HttpUtility.UrlEncode(AutoLogin.ToString()));
HttpContext.Current.Response.Cookies.Add(ck);
}
2、读取cookie
public void ReadCookies()
{
HttpCookie ck = HttpContext.Current.Request.Cookies["ckLogin"];
UserID = HttpUtility.UrlDecode(ck["cID"]);
UserPWD = HttpUtility.UrlDecode(ck["cPWD"]);
AutoLogin = Boolean.Parse(HttpUtility.UrlDecode(ck["cAutoLogin"]));
}
标签:
原文地址:http://www.cnblogs.com/dghxt/p/5220721.html