資料彙總整理-僅供個人學習使用
下面是寫入cookie的程式碼
HttpCookie cookie = new HttpCookie("username"); cookie.Value = "張三,14,images/1.jpg"; cookie.Expires = DateTime.Now.AddDays(1); Response.Cookies.Add(cookie);
下面是讀取cookie的程式碼
if (Request.Cookies["username"]!=null) { string username = Request.Cookies["username"].Value; Response.Write(username); }
有時讀取出來的cookie值中的中文部分可能是亂碼,不管是有什麼導致的,我們都可以通過編碼進行解決
更改上面寫入cookie的程式碼
HttpCookie cookie = new HttpCookie("username"); cookie.Value = HttpUtility.UrlEncode("張三,14,images/1.jpg",Encoding.GetEncoding("UTF=8")); cookie.Expires = DateTime.Now.AddDays(1); Response.Cookies.Add(cookie);
更改上面讀取cookie的程式碼
if (Request.Cookies["username"]!=null) { string username =HttpUtility.UrlDecode(Request.Cookies["username"].Value,Encoding.GetEncoding("UTF-8")); Response.Write(username); }
--------------------- 作者:加菲貓的狗 來源:CSDN 原文:https://blog.csdn.net/mynewdays/article/details/16119161 版權宣告:本文為博主原創文章,轉載請附上博文連結!