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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端