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:获得 ...
随机推荐
- appium中从activity切换到html
问题:混合开发的app中,会有内嵌的H5页面元素,该如何进行定位操作? 解决思路:appium中的元素定位都是基于android原生控件进行元素定位,而web网页是B/S架构,两者运行环境不同需要进行 ...
- zabbix server端与agent端源码安装 自定义监控项
ZabbixServer的安装(只有源码装zabbix才能装支持java) 搭建自定义yum仓库并安装支持包 yum -y install createrepo #下载依赖关系命令 createrep ...
- UML 2.5版本与UML分类概述
UML 2.5版本与UML分类概述 转 http://www.umlstudy.com/uml-25-diagrams.html UML简述 UML图是设计.实现或已经存在的系统模型的部分图形表示(视 ...
- React Native中Navigator的安装与使用
一.安装Navigator 1.安装react-native-deprecated-custom-components npm install react-native-deprecated-cust ...
- Handler处理消息
UI主线程通过Looper循环查询消息队列UI_MQ,当发现有消息存在时会将消息从消息队列中取出.首先分析消息,通过消息的参数判断该消息对应的Handler,然后将消息分发到指定的Handler进行处 ...
- PHP md5() 函数
PHP String 函数 实例 计算字符串 "Hello" 的 MD5 散列: <?php $str = "Shanghai"; echo md5($s ...
- php版本:实现过滤掉广告、色情、政治相关的敏感词
现在网络上还是很乱,尤其充斥着各种广告.色情.政治相关的内容,很明显这是不符合我们国家的法律的,所以为了一个产品能够健康长久的活下去,最好还是采用一定的策略过滤或者提醒用户不要发这种内容.不过说起来容 ...
- Qt编写自定义控件17-按钮进度条
前言 按钮进度条,顾名思义,表面上长得像一个按钮,单击以后切换成进度条指示按钮单击动作执行的进度,主要用在一些需要直接在按钮执行动作显示对应进度的场景,在很多网页中经常看到这种效果,这个效果有个优点就 ...
- docker版的zabbix部署
环境准备:一台server端,两台agent端 server端部署zabbix-server和mariadb服务 agent端部署zabbix-agent服务 一.docker容器里下载zabbix和 ...
- Spring Cloud(1):概览
什么是微服务? 小型的,简单的和解耦的服务 = 可伸缩的,有弹性的和灵活的应用程序. 什么是云? 基础设施即服务(Infrastructure as a Service, Iaas):云提供商只提供基 ...