asp.net Cookie value Chinese garbled problem

伊谢尔伦
Release: 2016-11-25 09:14:55
Original
1735 people have browsed it

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);
Copy after login

When reading the cookie:

if (Request.Cookies["name"] != null)
{
    Response.Write(System.Web.HttpContext.Current.Server.UrlDecode(Request.Cookies["name"].Value));
}
Copy after login

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


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template