Chinese cannot be written in the cookie. This is due to the innate encoding method of the cookie. So there needs to be an intermediate encoding to make the transition. URLEncode is the best choice.
We take asp.net as an example, the code is as follows:
When setting the cookie:
HttpCookie cookie = new HttpCookie("name", System.Web.HttpContext.Current.Server.UrlEncode("雨林星空")); Response.Cookies.Add(cookie);
When reading the cookie:
if (Request.Cookies["name"] != null) { Response.Write(System.Web.HttpContext.Current.Server.UrlDecode(Request.Cookies["name"].Value)); }
Note: The encoding and decoding must be consistent
System.Web.HttpContext.Current.Server.UrlDecode and System.Web.HttpContext.Current.Server.UrlEncode
System.Web.HttpUtility.UrlDecode and System.Web.HttpUtility.UrlEncode