惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Palo Alto Networks Blog
C
Cisco Blogs
The Hacker News
The Hacker News
T
Threatpost
S
Schneier on Security
K
Kaspersky official blog
Spread Privacy
Spread Privacy
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
NISL@THU
NISL@THU
量子位
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google DeepMind News
Google DeepMind News
Security Latest
Security Latest
博客园 - 司徒正美
云风的 BLOG
云风的 BLOG
博客园 - 叶小钗
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
News and Events Feed by Topic
爱范儿
爱范儿
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
Project Zero
Project Zero
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cisco Talos Blog
Cisco Talos Blog
GbyAI
GbyAI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Apple Machine Learning Research
Apple Machine Learning Research
T
Tenable Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Vulnerabilities – Threatpost
Forbes - Security
Forbes - Security
博客园 - 三生石上(FineUI控件)
C
Cyber Attacks, Cyber Crime and Cyber Security
N
News and Events Feed by Topic
V
V2EX
Webroot Blog
Webroot Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Blog — PlanetScale
Blog — PlanetScale
M
MIT News - Artificial intelligence
Scott Helme
Scott Helme
Simon Willison's Weblog
Simon Willison's Weblog
L
LangChain Blog
W
WeLiveSecurity
Cloudbric
Cloudbric

博客园 - 黑*马

和老外吵架必备的108句英语! Sql server常见性能问题的总结 asp.net2.0导出pdf文件完美解决方案(转载) (转)asp.net 2.0中页的生存周期(Lifecycle)和动态控件 我来试试trackback的用法 PetShop介绍集锦 C#基础概念二十五问(转) [转贴]GOF 23种模式MM版 in和exists的区别与SQL执行效率分析 Transact-SQL语句总汇 ASP.NET里的事务处理(ado.net 数据库应用) .net 三层结构基础理论知识(高手勿进) 后台脚本实现页面在target中的打开,以及target属性 什么是用户体验(ue),以及用户体验的关键 网站架构 什么是Web 2.0(与Web 1.0比较)简单版 微软吹大话?SP1并没提高Windows Vista运行速度 xml学习:有什么优势和特点及常见问答加深理解 你真的了解四核处理器吗?告诉你十点
c#.net在WEB页中设置COOKIES - 黑*马 - 博客园
黑*马 · 2008-03-21 · via 博客园 - 黑*马

一、设置cookies的方法很简单,有以下两种方法:

1、直接添加Cookie值:

Response.Cookies["userName"] = "Tom"; 
Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1) ; \\过期时间,在Cookies文件中无法查看,也不能调用.

2、创建Cookie对象的一个实例:

HttpCookie cookie=new HttpCookie("userName"); 
cookie.Value = "Tom"; 
cookie.Expires = DateTime.Now.AddDays(1) ; 
Response.Cookies.Add(aCookie)

用以上任一方法都可以生成一个有“userName”项的文件, 在你的Internet临时文件夹中你可以查看它。

也可以创建和添加有子键的Cookies,如:

Response.Cookies["userInfo"]["userName"] = "Tom"; 

或:

HttpCookie cookie=new HttpCookie("userInfo"); 
cookie.Values["userName"] = "Tom"; 
aCookie.Expires = DateTime.Now.AddDays(1); 
Response.Cookies.Add(aCookie)

二、检索Cookies:

Cookies某一键的值为:

Server.HtmlEncode(Request.Cookies["userInfo"]["userName"])

你可以用Response.Write()方法输出它到页面,如:

Response.Write(Server.HtmlEncode(Request.Cookies["userInfo"]["userName"]));

或赋值给其它变量:

string strCookie1=Server.HtmlEncode(Request.Cookies["userInfo"]["userName"]); 

用Cookies[i]数组可以检索所有项和子键,如:

string[] cooName = new string[Request.Cookies.Count]; 
string[] cooValue = new string[Request.Cookies.Count]; 
HttpCookie aCookie; 
for(int i=0; i aCookie = Request.Cookies[i]; 
cooName[i] = Server.HtmlEncode(aCookie.Name); 
if(!aCookie.HasKeys){
cooValue[i] = Server.HtmlEncode(aCookie.Value); 
}else{
string[] subcooName = new string[aCookie.Values.Count]; 
string[] subcooValue = new string[aCookie.Values.Count]; 
for(int j=0; j subcooName[j] = Server.HtmlEncode(aCookie.Values.AllKeys[j]); 
subcooValue[j] = Server.HtmlEncode(aCookie.Values[j]); 
}
}
}

三、修改Cookies

如果是数值类型的Cookie值,比如访问次数,你可以读取该值进行加减操作后再存回,一般的修改直接存入新值就可以了,系统自动用新值覆盖原值,存入的方法与创建相同。

四、删除Cookies

删除Cookies只要把有效期设为失效就可以了,如在创建时设有效期为一天:
cookie.Expires = DateTime.Now.AddDays(1) ; 

要删除则设为:

cookie.Expires = DateTime.Now.AddDays(-1) ; 

删除子键:
HttpCookie cookie; 
cookie = Request.Cookies["userInfo"]; 
aCookie.Values.Remove("userName"); 
aCookie.Expires = DateTime.Now.AddDays(1); 
Response.Cookies.Add(aCookie);