cookie里面不能寫(xiě)中文,是由于cookie先天的編碼方式造成的。所以需要有一種中間編碼來(lái)過(guò)渡。 URLEncode是最好的選擇。
我們以asp.net為例,代碼如下:
設(shè)置Cookie時(shí):
復(fù)制代碼代碼如下:
HttpCookie cookie = new HttpCookie("name", System.Web.HttpContext.Current.Server.UrlEncode("服務(wù)器之家"));
Response.Cookies.Add(cookie);讀取Cookie時(shí):
if (Request.Cookies["name"] != null)
{
Response.Write(System.Web.HttpContext.Current.Server.UrlDecode(Request.Cookies["name"].Value));
}
注意:編碼和解碼要一致
復(fù)制代碼代碼如下:
System.Web.HttpContext.Current.Server.UrlDecode 和 System.Web.HttpContext.Current.Server.UrlEncode
System.Web.HttpUtility.UrlDecode 和 System.Web.HttpUtility.UrlEncode