C#中操作单个cookie和cookie字典

2021-04-13 08:25

阅读:531

YPE html>

id="Label3" contentScore="722">

标签:his   color   onclick   space   char   lse   分享图片   val   load   

单个cookie和cookie字典在浏览器中的存储格式如下:
技术分享图片
可以看到,单个cookie是以单一键值对的方式存储的,而cookie字典的值则是以多个&符号相连的。
cookie字典用于用单个cookie保存多个值的情况。

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




    

 .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字典


 .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

C#中操作单个cookie和cookie字典

标签:his   color   onclick   space   char   lse   分享图片   val   load   

原文地址:https://www.cnblogs.com/zhaow/p/8981744.html


评论


亲,登录后才可以留言!