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

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

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <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>
</body>
</html>

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <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>
</body>
</html>

.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

最新文章

  1. bzoj 1179[Apio2009]Atm (tarjan+spfa)
  2. Java总结篇系列:Java多线程(一)
  3. 【MongoDB:第二天】基本操作
  4. js使用模板快速填充数据
  5. node与mongodb的使用
  6. Hadoop_FileInputFormat分片
  7. GUID
  8. sort,uniq命令
  9. Spring MVC 项目搭建 -1- 创建项目
  10. SAP smartform 实现打印条形码
  11. FCC学习笔记(一)
  12. 程序员50题(JS版本)(六)
  13. 浅谈TCP IP协议栈(二)IP地址
  14. Linux shell编程语法
  15. Python_记一次网站数据定向爬取实现
  16. 轻松3步设置sublime text2/sublime text3的预览浏览器
  17. CF633H Fibonacci-ish II 莫队、线段树、矩阵乘法
  18. C# 对象与JSON字符串互相转换的三种方式
  19. powerdesigner 字段添加注释和默认值
  20. build.gradle最佳实践之buildConfigField

热门文章

  1. Ubuntu 安装 JDK1.8
  2. antd源码分析之——折叠面板(collapse)
  3. Could not attach to a Hearthstone process.
  4. 【互联网运营P1】
  5. Android数据存储的方式
  6. 前端单元测试,以及给现有的vue项目添加jest + Vue Test Utils的配置
  7. java IO流的API
  8. iscsi序列二、iscsi多路径配置方式
  9. Threadlocal源码分析以及其中WeakReference作用分析
  10. iOS开发UIkit动力学UIDynamicAnimator一系列动画