单个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

C#中操作单个cookie和cookie字典的更多相关文章

  1. django中操作cookie与session

    cookie 什么是Cookie Cookie具体指的是一段小信息,它是服务器发送出来存储在浏览器上的一组组键值对,下次访问服务器时浏览器会自动携带这些键值对,以便服务器提取有用信息. Cookie的 ...

  2. {Django基础八之cookie和session}一 会话跟踪 二 cookie 三 django中操作cookie 四 session 五 django中操作session

    Django基础八之cookie和session 本节目录 一 会话跟踪 二 cookie 三 django中操作cookie 四 session 五 django中操作session 六 xxx 七 ...

  3. thinkphp中cookie和session中操作数组的方法

    thinkphp中cookie和session中操作数组的方法 一.ThinkPHP模板中如何操作session,以及如果session中保存的是数组的情况 在ThinkPHP的模板中操作sessio ...

  4. 135.在django中操作cookie

    操作cookie 设置cookie 设置cookie是设置值给浏览器的,因此我们可以通过response的对象来设置,可以通过HttpResponse的对象或者是HttpResponseBase的子类 ...

  5. Java中Cookie常用操作类(Spring中操作Cookie)

    说明:Cookie下用Key取值没有快速的方法,只能便利循环去取. 技巧:置0则cookie会立即删除,设置-1,负值则会在关闭浏览器后删除.切记一定要增加路径:setPath("/&quo ...

  6. Asp.Net中用JS中操作cookie的方法

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="cookies.aspx.cs& ...

  7. selenium 操作cookie (cookie测试)

    前言 在实际的web应用中,可能会涉及到cookie测试,验证浏览器中的cookie是否正确..Cookies 验证:如果系统使用了cookie,测试人员需要对它们进行检测.如果在 cookies 中 ...

  8. NET中Application,Session,Cookie,ViewState,Cache,Hidden 缓存机制 .

    Application 1.         Application用来保存所有用户共用的信息 2.         在Asp时代,如果要保存的数据在应用程序生存期内不会或者很少发生改变,那么使用Ap ...

  9. ThinkPHP第二十六天(JQuery操作select,SESSION和COOKIE)

    1.JQuery操作select,假设<select id="my"> A:双击选项<option>事件,应该是select的dbclick事件. B:获得 ...

随机推荐

  1. start-20180323

    几年前申请了博客,http://www.cnblogs.com/cdfive/,一篇文章没写-_-|| 账号都忘了orz.. 又到了离职的时候,开始重新找工作: 昨天一家平台好的单位面试没过,可能是跳 ...

  2. Android ROM适配

    Android是开源的,不同的手机厂商都有自己定制的系统,所以这就给开发者带来了ROM适配难题.在一些群里面经常看到有人因为手机适配问题,说这个手机坑,那个手机坑,其实那是没有对ROM定制系统的一些变 ...

  3. 机器学习 - 算法 - 集成算法 - 分类 ( Bagging , Boosting , Stacking) 原理概述

    Ensemble learning - 集成算法 ▒ 目的 让机器学习的效果更好, 量变引起质变 继承算法是竞赛与论文的神器, 注重结果的时候较为适用 集成算法 - 分类 ▒ Bagging - bo ...

  4. curl获取远程页面

    // 创建一个新cURL资源$ch = curl_init(); // 设置URL和相应的选项$options = array(CURLOPT_URL => 'http://www.w3csch ...

  5. AngularJS ng-disabled在a内无效

    在AngularJS中,对a添加ng-disabled,在disabled情况下,虽显示了不可用的样式,但点击了依旧能触发绑定在a上的ng-click事件. 解决方式:将a改为button.

  6. Selenium 2自动化测试实战36(更易读的测试报告)

    一.更易读的测试报告 1.知识点:python的注释. 1.一种叫comment,为普通的注释2.另一种叫doc string,用于函数,类和方法的描述.在类或方法的下方,通过三引号("&q ...

  7. Ubuntu vmware补丁

    获取补丁文件 curl http://pastie.org/pastes/9934018/download -o /tmp/vmnet-3.19.patch 如果无法下载,云盘上取,网址为:http: ...

  8. 1. hadoop使用启动命令时报错之分析解决

    今天在学习hadoop启动命令的时候,先jps看了下,发现namenode.datanode都开着,所以想要先停止这些服务,结果输入命令后报错:“WARN util.NativeCodeLoader: ...

  9. React Native中Touchable组件的使用

    截图如下: /** * Sample React Native App * https://github.com/facebook/react-native * @flow */ import Rea ...

  10. 【Linux】配置SSH免密登录

    环境说明 假设我们有三台机器分别为bigdata111,bigdata112,bigdata113,三台机器均为centos 7系统. 配置SSH免密登录 (1)利用Xshell的发送键输入到所有会话 ...