Home  >  Article  >  Backend Development  >  asp.net Cookie value Chinese garbled problem

asp.net Cookie value Chinese garbled problem

伊谢尔伦
伊谢尔伦Original
2016-11-25 09:14:551697browse

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


Statement:
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