Today, a *** asked me: why is the read value garbled when I write Chinese Characters in Asp.net's httpcookie? In fact, this is caused by text encoding. Chinese characters are encoded in two ways, so such a garbled code is generated! In fact, the solution is very simple: as long as the cookie is written, it will first use URL encoding and then write, and then it will be OK when we read it, hope more *** sisters can solve it!
Example:
Cookie writing: 1 Httpcookie = New Httpcookie ( " Simple " );
2 Cookie. Values. Add ( " Simple1 " , Httputility. urlencode ( " Hi, Uncle! " ));
3 Cookie. Values. Add ( " Simple2 " , " English is OK! Nothing we shoshould do! " );
4 Response. appendcookie (cookie );
Cookie reading: 1 Httpcookie = Request. Cookies [ " Simple " ];
2 String Simple1 = Httputility. urldecode (cookie [ " Simple1 " ]);
3 String Simple2 = Cookie [ " Simple2 " ];
So simple1 = "Hello, uncle! "; Simple2 ="English is OK! Nothing we shoshould do!";
Easy !!!