C#中操作单个cookie和cookie字典
阅读原文时间:2023年07月11日阅读:1

单个cookie和cookie字典在浏览器中的存储格式如下:

可以看到,单个cookie是以单一键值对的方式存储的,而cookie字典的值包含多个键值对,这些键值对之间以&符号拼接。
cookie字典用于用一个cookie保存多个值的情况。

下面是单个cookie和cookie字典的操作示例:
1、单个cookie






        <br />  
        <asp:Button ID="Button1" runat="server" OnClick="Button1\_Click" Text="设置单个cookie" />  
        <asp:Button ID="Button2" runat="server" OnClick="Button2\_Click" Text="获取单个cookie" />  
        <asp:Button ID="Button3" runat="server" OnClick="Button3\_Click" Text="修改单个cookie" />  
    </div>  
</form>  


.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
public partial class CookieSingle : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

    }

    protected void Button1\_Click(object sender, EventArgs e)  
    {  
        HttpCookie cookie = new HttpCookie("xfk\_sid")  
        {  
            Value = "xfk11111111",  
            Expires = DateTime.Now.AddDays(1)  
        };  
        Response.Cookies.Add(cookie);  
    }

    protected void Button2\_Click(object sender, EventArgs e)  
    {  
        var objCookie = Request.Cookies\["xfk\_sid"\];  
        if (objCookie != null)  
        {  
            this.Label1.Text += objCookie.Value + "----";  
        }  
    }

    protected void Button3\_Click(object sender, EventArgs e)  
    {  
        HttpCookie c1 = Request.Cookies\["xfk\_sid"\];  
        c1.Value = "after1111111111";  
        Response.Cookies.Add(c1);  
    }  
}  

}

2、cookie字典






        <br />  
        <asp:Button ID="Button1" runat="server" OnClick="Button1\_Click" Text="设置cookie字典" />  
        <asp:Button ID="Button2" runat="server" OnClick="Button2\_Click" Text="获取cookie字典" />  
        <asp:Button ID="Button3" runat="server" OnClick="Button3\_Click" Text="修改cookie字典" />  
    </div>  
</form>  


.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
public partial class CookieDict : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

    }

    protected void Button1\_Click(object sender, EventArgs e)  
    {  
        HttpCookie cookie = new HttpCookie("xfk\_sidDict");  
        cookie.Values.Add("s1", "ssssssssss");  
        cookie.Values.Add("s2", "iiiiiiiii");  
        cookie.Values.Add("s3", "ddddddddddd");  
        Response.Cookies.Add(cookie);  
    }

    protected void Button2\_Click(object sender, EventArgs e)  
    {  
        HttpCookie cookie = Request.Cookies\["xfk\_sidDict"\];  
        if (cookie != null && cookie.HasKeys) {  
            foreach (string item in cookie.Values)  
            {  
                this.Label1.Text += "---" + cookie.Values\[item\];  
            }  
        }  
    }

    protected void Button3\_Click(object sender, EventArgs e)  
    {  
         HttpCookie cookie = Request.Cookies\["xfk\_sidDict"\];  
         if (cookie != null && cookie.HasKeys) {  
             cookie.Values.Set("s1", "hahahahahah");  
             cookie.Values.Set("s3", "heiheiheiheihi");  
             Response.Cookies.Add(cookie);  
         }  
    }  
}  

}

参考:https://www.cnblogs.com/chenlihong-886/articles/6234535.html

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章