在存之前和取出前后进行编码和解码
string str = "中文内容";
byte[] utf8Buf = Encoding.UTF8.GetBytes(str);
byte[] gbkBuf = Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding("GBK"), utf8Buf);
string str1 = Encoding.GetEncoding("GBK").GetString(gbkBuf);
ASP.NET(C#):使用HttpUtility.UrlDecode解决cookies的中文乱码
保存cookie:
//写入cookie
cookie.Values.Add("Userid", HttpUtility.UrlDecodeTextBox1.Text)); Response.AppendCookie(cookie);
// 读取cookie:
TextBox2.Text = HttpUtility.UrlDecode(Request.Cookies["Info"].Values["Userid"]);
用escape和unescape进行编码和解码就可以了
写cookie时escape一下,取的时候unescape一下