The solution of ASP.net's httpcookie writing to read Chinese characters as garbled

Source: Internet
Author: User
asp.net|cookie| Chinese Characters | solving

Today, a student sister asked me: In the asp.net HttpCookie write Chinese characters, read the value of why are all garbled? In fact, this is because of the text encoding, the Chinese character is two code, so it will get out of such a garbled! In fact, the solution is very simple: as long as the cookie is written, the first URL code, and then write, when we read and then decode the OK, hope that more students sister can solve it, hehe!

Example:

Write to Cookie:

1HttpCookie cookie=new HttpCookie ("simple");
2cookie. Values.add ("Simple1", Httputility.urlencode ("Uncle, Hello!") "));
3cookie. Values.add ("Simple2", "中文版 is ok! Nothing we should do! ");
4response.appendcookie (cookie); Read the cookie:
1HttpCookie cookie=request.cookies["Simple"];
2string Simple1=httputility.urldecode (cookie["Simple1"]);
3string simple2=cookie["Simple2"]; So simple1= "uncle, Hello!" '; simple2= ' Chinese is ok! Nothing we should do! ";

It's very simple!!!



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.