zoukankan      html  css  js  c++  java
  • Cookie的创建、读取、删除

    创建Cookie:

    HttpCookie cookie =  new HttpCookie(COOKIE_NAME_FOR_USER);
    cookie.Expires = DateTime.Now.AddYears(1);//设置日期
    cookie[COOKIE_USER_NAME] = "用户名";
    System.Web.HttpContext.Current.Response.Cookies.Add(cookie);

    读取Cookie:

    HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies.Get(COOKIE_NAME_FOR_USER);


    删除Cookie:

    System.Web.HttpContext.Current.Request.Cookies.Remove(COOKIE_NAME_FOR_USER)

    另外,判断是否有cookie可以直接使用取的cookie和null进行比较,如果取出来非null就说明不为空

    另外在一般处理程序中的使用方法:

      //写入cookie
      HttpCookie hc = new HttpCookie("time");
      hc.Value = DateTime.Now.ToString();
      hc.Expires = DateTime.Now.AddDays(7);
      context.Response.Cookies.Add(hc);

    //读取Cookie
    HttpCookie hc = context.Request.Cookies["time"]; if (hc != null) { context.Response.Write(hc.Value); } else { context.Response.Write("meiyou "); }


      //删除cookie 就是把cookie设置过期输出
      int count = context.Request.Cookies.Count;

      for (int i = 0; i < count; i++)
      {
         HttpCookie hc = context.Request.Cookies[i];     
         hc.Expires = DateTime.Now.AddDays(-1); //设置过期 
         context.Response.Cookies.Add(hc);
      }

  • 相关阅读:
    企业组织与经营管理
    VS2008试用版到期解决办法
    [分期付款] 建行安居分期,刚申请过,最高额度居然能申请到100万!汗。。
    [手游新项目历程]-29- windows-PC与 linux虚拟机交互
    诛仙手游培元属性
    [Warning] TIMESTAMP with implicit DEFAULT value
    bzoj3631
    bzoj1257
    bzoj1041
    bzoj1560
  • 原文地址:https://www.cnblogs.com/xbblogs/p/4721476.html
Copyright © 2011-2022 走看看