C# 有关windows2003服务器端中文 cookies 乱码解决

  1. public void SetMyCookie(string name, string vals,double  extime)
  2.     {
  3.         HttpCookie usercookie = new HttpCookie(name, System.Web.HttpUtility.UrlEncode(vals, System.Text.Encoding.GetEncoding("utf-8")));
  4.         usercookie.Expires = DateTime.Now.AddDays(extime);
  5.         HttpContext.Current.Response.Cookies.Add(usercookie);
  6.        // HttpContext.Current.Response.Cookies.Add(new HttpCookie(name, System.Web.HttpUtility.UrlEncode(vals, System.Text.Encoding.GetEncoding("utf-8"))));
  7.     }

3个参数, 有 cookie的  名 ,值 ,过期时间。

调用时 如下:

  1.  SetMyCookie("Name","javc",1);                  

我们还需要一个取出cookie的函数 ,如下:

  1.  public string GetMyCookie(string name)
  2.     {
  3.         if (HttpContext.Current.Request.Cookies[name] != null)
  4.         {
  5.             return System.Web.HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[name].Value, System.Text.Encoding.GetEncoding("utf-8"));
  6.         }
  7.         else
  8.         {
  9.           //如果需要异常处理 
  10.           // Response.Write("<script type='text/javascript' language='javascript'>");
  11.           //  Response.Write("alert('请重新登陆!//n//n将被引导首页面');");
  12.           //  Response.Write(" parent.location='./login.aspx';");
  13.           // Response.Write("</script>");
  14.           //  Response.End();
  15.           //不需要 可自己返回空
  16.             return "";
  17.         }
  18.     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值