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:获得 ...
随机推荐
- 前端知识点回顾——Javascript篇(六)
fetch 在原生ajax+es6promise的基础上封装的一个语法糖,返回promise对象. fetch(url, initObj) .then(res=>res.json()) .the ...
- centos6里面装zabbix(五)
今天说使用ICMP ping监控server与agent端的网络状态 今天要使用的是fping,这个软件包需要去官网下载,官网地址:http://www.fping.org/.现在的最新版是4.0 第 ...
- 你真的理解Java中的try/catch/finally吗?
看几个例子,回顾一下执行顺序 例子1 无异常,finally中的return会导致提前返回 public static String test() { try { System.o ...
- C# AES的128位、192位、256位加密
C# AES的128位.192位.256位加密 AES加密原理,这里就不解释了,自行百度.这里主要细说AES的CBC加密模式下的128位.192位.256位加密区别,参考 对称加密和分组加密中的四 ...
- 来聊聊JavaScript中的防抖和节流
目录 JavaScript防抖和节流 问题还原 防抖 什么是防抖 使用场景 节流 什么是节流 使用场景 JavaScript防抖和节流 问题还原 我们先来通过代码把常见的问题还原: <html& ...
- vue中 key 值的作用
原文地址 我们知道,vue和react都实现了一套虚拟DOM,使我们可以不直接操作DOM元素,只操作数据便可以重新渲染页面.而隐藏在背后的原理便是其高效的Diff算法. vue和react的虚拟DOM ...
- python-Web-django-商城-购物车商品加减
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Java 基础篇之lambda
Lambda 示例 public interface Eatable { void taste(); } public interface Flyable { void fly(String weat ...
- typescript实现类规则
备注: 单独的 index.d.ts对于代码实现没有约束性,将约束和实现写在一个页面里有约束性,或者使用如下: // clock.interface.ts export interface Clock ...
- #内存不够,swap来凑# Linux上创建SWAP文件/分区
转自:https://www.vmvps.com/how-to-create-a-swap-file-on-the-linux-os.html 很久很久以前,电脑的内存是个珍贵东西,于是乎就有了swa ...