zoukankan      html  css  js  c++  java
  • Cookie的增删改查

    增加:

    第一种方法:Response.Cookies[“UserName”].Value=”张三”

    Response.Cookies[“UserName”].Expires=DateTime.Now.AddDays(1);

    第二种方法:

    HttpCookie cookie=new HttpCookie(“UserName”);

    cookie.Value=”张三”;

    cookie.Expires=DateTime.Now.AddDays(1);

    Response.Cookies.Add(cookie);

    多值Cookie(子键):

    第一种方法:

    Response.Cookies[“UserInfo”][“userName”]=”张三”;

    Response.Cookies[“UserInfo”][“pwd”]=”123”;

    Response.Cookies[“UserInfo”].Expires=DateTime.Now.AddDays(1);

    第二种方法:

    HttpCookie cookie=new HttpCOokie(“UserInfo”);

    cookie.Values(“userName”)=”张三”;

    cookie.Values(“pwd”)=”123”;

    cookie.Expires=DateTime.Now.AddDays(1);

    Response.Cookies.Add(cookie);

    删除:

    删除单个Cookie

    Response.Cookies[“userName”].Expires=DateTime.Now.AddDays(-1);

    删除所有的Cookie

    HttpCookie aCookie;

    String cookieName;

    int count=Request.Cookies.Count;

    for(int i=0;i<count;i++)

    {

           cookieName=Request.Cookies[i].Name;

           aCookie=new HttpCookie(cookieName);

           aCookie.Expires=DateTime.Now.AddDays(-1);

           Response.Cookies.Add(aCookie);

    }

    删除单个子键

    HttpCookie aCookie=Request.Cookies[“userInfo”];

    aCookie.Values.Remove(“userName”);

    aCookie.Expires=DateTime.Now.AddDays(-1);

    Response.Cookies.Add(aCookie);

    修改:

    修改单个Cookie

    Response.Cookies[“userName”].Value=”李四”;

    Response.Cookies[“userName”].Expires=DateTime.Now.AddDays(1);

    修改单个子键

    Response.Cookies[“UserInfo”][“pwd”]=”234”;

    Response.Cookies[“UserInfo”].Expires=DateTime.Now.AddDays(1);

    读取:

    If(Request.Cookies[“userName”]!=null)

    {

           Lable1.Text=Request.Cookies[“userName”].Value;

    }

    或者

    If(Request.Cookies[“userName”]!=null)

    {

           HttpCookie cookie=Request.Cookies[“userName”];

           Lable.Text=cookie.Value;

    }

    多值读取

    If(Request.Cookies[“UserInfo”]!=null)

    {

           Lable1.Text=Request.Cookies[“UserInfo”][“userName”];

    Lable2.Text=Request.Cookies[“UserInfo”][“pwd”];

    }

    读取所有的Cookie

    for(int i=0;i<Request.Cookies.Count;i++)

    {

           aCookie=Request.Cookies[i];

           string name=aCookie.Name;

           string value=aCookie.Value;

    }

  • 相关阅读:
    时间序列表示
    Resnet——深度残差网络(二)
    Resnet——深度残差网络(一)
    基于tensorflow2.0和cifar100的VGG13网络训练
    Android实战项目——家庭记账本(七)
    iOS-基于TCP连接<Scoket-客户端>
    Xcode更新到10.0之后遇到的那些坑:
    cell右侧的状态(accessoryType)
    2020最新cocoapods详细安装和使用
    AFNetworking上传一张或多张图片,并压缩图片节约占用内存
  • 原文地址:https://www.cnblogs.com/zhuyuchao/p/5634237.html
Copyright © 2011-2022 走看看