仰天一笑(Ansonxuyu),专业从事软件定制开发、Web软件开发,网站建设,网络推广,APP开发,微博应用开发,微信应用开发,电子商务开发,物联网开发等技术。
互联网8年风雨,愿在此交朋识友,交流心得,分享技术知识(策划/研发/运营/推广/合作)!QQ:943530498


仰天一笑

昨日不悔,今日勿失,明日莫忧! —徐羽

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

// 写cookie
HttpCookie cookie = new HttpCookie("Info");//定义cookie对象以及名为Info的项
DateTime dt = DateTime.Now;//定义时间对象
TimeSpan ts=new TimeSpan(1,0,0,0);//cookie有效作用时间,具体查msdn
cookie.EXPires = dt.Add(ts);//添加作用时间
cookie.Values.Add("user","cxbkkk");//增加属性
cookie.Values.Add("userid","1203");
Response.AppendCookie(cookie);//确定写入cookie中        读取cookie
if(Request.Cookies["Info"]!=null)
{
     string temp=Convert.ToString(Request.Cookies["Info"].Values["user"])+"  "+Convert.ToString(Request.Cookies["Info"].Values["userid"]);
    //读全部就用Request.Cookies["Info"].Value)
     if(temp=="")
     {
         Response.Write("空");
     }
     else
        Response.Write(temp);
}
else
{
    Response.Write("error");
}        修改cookie
Response.Cookies["Info"]["user"] = "2";
Response.Cookies["Info"].Expires = DateTime.Now.AddDays(1);      

// 删除cookie下的属性
HttpCookie acookie=Request.Cookies["Info"];
acookie.Values.Remove("userid");
acookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(acookie);        删除所有cookie,就是设置过期时间为现在就行了
int limit=Request.Cookies.Count - 1;
for(int i=0;i<limit;i++)
{
   acookie = Request.Cookies(i)
   acookie.Expires = DateTime.Now.AddDays(-1)
   Response.Cookies.Add(acookie)
}


// 读取所有 cookies
HttpCookieCollection cookies;
HttpCookie oneCookie;
cookies = Request.Cookies;

string[] cookieArray = cookies.AllKeys;
for (int i=0; i < cookieArray.Length; i++)
{
 oneCookie = cookies[cookieArray[i]];
 Response.Write(oneCookie.Name + " - " + oneCookie.Value);
}

// 添加单个cookies
if (Request.Browser.Cookies == true)
{
 if (Request.Cookies["LastVisited1"] == null)
 {
  HttpCookie newCookie = new HttpCookie("LastVisited1",DateTime.Now.ToString());
  newCookie.Expires = DateTime.Now.AddYears(1);
  Response.Cookies.Add(newCookie);
 }
 else
 {
  Response.Write(Request.Cookies["LastVisited1"].Value);
 } 
}

posted on 2007-09-04 18:07  仰天一笑  阅读(3143)  评论(1编辑  收藏  举报