C#中操作单个cookie和cookie字典
单个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字典的更多相关文章
- django中操作cookie与session
cookie 什么是Cookie Cookie具体指的是一段小信息,它是服务器发送出来存储在浏览器上的一组组键值对,下次访问服务器时浏览器会自动携带这些键值对,以便服务器提取有用信息. Cookie的 ...
- {Django基础八之cookie和session}一 会话跟踪 二 cookie 三 django中操作cookie 四 session 五 django中操作session
Django基础八之cookie和session 本节目录 一 会话跟踪 二 cookie 三 django中操作cookie 四 session 五 django中操作session 六 xxx 七 ...
- thinkphp中cookie和session中操作数组的方法
thinkphp中cookie和session中操作数组的方法 一.ThinkPHP模板中如何操作session,以及如果session中保存的是数组的情况 在ThinkPHP的模板中操作sessio ...
- 135.在django中操作cookie
操作cookie 设置cookie 设置cookie是设置值给浏览器的,因此我们可以通过response的对象来设置,可以通过HttpResponse的对象或者是HttpResponseBase的子类 ...
- Java中Cookie常用操作类(Spring中操作Cookie)
说明:Cookie下用Key取值没有快速的方法,只能便利循环去取. 技巧:置0则cookie会立即删除,设置-1,负值则会在关闭浏览器后删除.切记一定要增加路径:setPath("/&quo ...
- Asp.Net中用JS中操作cookie的方法
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="cookies.aspx.cs& ...
- selenium 操作cookie (cookie测试)
前言 在实际的web应用中,可能会涉及到cookie测试,验证浏览器中的cookie是否正确..Cookies 验证:如果系统使用了cookie,测试人员需要对它们进行检测.如果在 cookies 中 ...
- NET中Application,Session,Cookie,ViewState,Cache,Hidden 缓存机制 .
Application 1. Application用来保存所有用户共用的信息 2. 在Asp时代,如果要保存的数据在应用程序生存期内不会或者很少发生改变,那么使用Ap ...
- ThinkPHP第二十六天(JQuery操作select,SESSION和COOKIE)
1.JQuery操作select,假设<select id="my"> A:双击选项<option>事件,应该是select的dbclick事件. B:获得 ...
随机推荐
- linux服务之redis
redis wget http://download.redis.io/releases/redis-4.0.6.tar.gzcd redis-4.0.6makecd src/./redis-serv ...
- spring + mybatis + mysql/oracle开发
1)创建一个spring-mybatis-mysql这么一个javaweb或java工程 2)导入spring-ioc,spring-aop,spring-transaction,mybatis,c3 ...
- bind绑定服务的生命周期
bindService(service, conn, flags); * service :意图 * conn :activity和服务的连接通道 * flags : BIND_AUTO_CREATE ...
- selenium webdriver常用函数
from selenium import webdriver driver = webdriver.Ie(executable_path = "e:\\IEDriverServer" ...
- kettle记录集(Merge Join)使用
果两个表需要进行连接查询筛选出数据,那么可以使用记录集组件.(Merge Join). 使用之前要进行排序: 使用记录集之前,要对输入的两张表进行排序,如果不排序,会导致两张表连接的时候出现问题,关联 ...
- Linux命令集锦:ssh命令
保持连接配置服务端SSH总是被强行中断,导致效率低下,可以在服务端配置,让 server 每隔30秒向 client 发送一个 keep-alive 包来保持连接: vim /etc/ssh/sshd ...
- .Netcore 2.0 Ocelot Api网关教程(4)- 服务发现
本文介绍Ocelot中的服务发现(Service Discovery),Ocelot允许指定一个服务发现提供器,之后将从中寻找下游服务的host和port来进行请求路由.关于服务发现的详细介绍请点击. ...
- vue中自定义指令的使用
原文地址 vue中除了内置的指令(v-show,v-model)还允许我们自定义指令 想要创建自定义指令,就要注册指令(以输入框获取焦点为例) 一.注册全局指令: // 注册一个全局自定义指令 `v- ...
- C语言递归之二叉树的最大深度
题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例 给定二叉树 [3,9,20,null,null,15 ...
- etcd配置参数详解
针对ETCD版本 3.2.17 --name 节点名称 default: "default" env variable: ETCD_NAME 这个值和--initial-clust ...