c# .net在WEB頁(yè)中的COOKIES設(shè)置技巧
更新時(shí)間:2011年07月11日 22:23:32 作者:
c# .net在WEB頁(yè)中的COOKIES設(shè)置技巧,需要的朋友可以參考下。
一、設(shè)置cookies的方法很簡(jiǎn)單,有以下兩種方法:
1、直接添加Cookie值:
Response.Cookies["userName"] = "Tom";
Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1) ; \\過期時(shí)間,在Cookies文件中無法查看,也不能調(diào)用.
2、創(chuàng)建Cookie對(duì)象的一個(gè)實(shí)例:
HttpCookie cookie=new HttpCookie("userName");
cookie.Value = "Tom";
cookie.Expires = DateTime.Now.AddDays(1) ;
Response.Cookies.Add(aCookie)
用以上任一方法都可以生成一個(gè)有“userName”項(xiàng)的文件, 在你的Internet臨時(shí)文件夾中你可以查看它。
也可以創(chuàng)建和添加有子鍵的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()方法輸出它到頁(yè)面,如:
Response.Write(Server.HtmlEncode(Request.Cookies["userInfo"]["userName"]));
或賦值給其它變量:
string strCookie1=Server.HtmlEncode(Request.Cookies["userInfo"]["userName"]);
用Cookies[i]數(shù)組可以檢索所有項(xiàng)和子鍵,如:
string[] cooName = new string[Request.Cookies.Count];
string[] cooValue = new string[Request.Cookies.Count];
HttpCookie aCookie;
for(int i=0;i<Request.Cookies.Count;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<aCookie.Values.Count;j++){
subcooName[j] = Server.HtmlEncode(aCookie.Values.AllKeys[j]);
subcooValue[j] = Server.HtmlEncode(aCookie.Values[j]);
}
}
}
三、修改Cookies
如果是數(shù)值類型的Cookie值,比如訪問次數(shù),你可以讀取該值進(jìn)行加減操作后再存回,一般的修改直接存入新值就可以了,系統(tǒng)自動(dòng)用新值覆蓋原值,存入的方法與創(chuàng)建相同。
四、刪除Cookies
刪除Cookies只要把有效期設(shè)為失效就可以了,如在創(chuàng)建時(shí)設(shè)有效期為一天:
cookie.Expires = DateTime.Now.AddDays(1) ;
要?jiǎng)h除則設(shè)為:
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);
1、直接添加Cookie值:
Response.Cookies["userName"] = "Tom";
Response.Cookies["userName"].Expires = DateTime.Now.AddDays(1) ; \\過期時(shí)間,在Cookies文件中無法查看,也不能調(diào)用.
2、創(chuàng)建Cookie對(duì)象的一個(gè)實(shí)例:
HttpCookie cookie=new HttpCookie("userName");
cookie.Value = "Tom";
cookie.Expires = DateTime.Now.AddDays(1) ;
Response.Cookies.Add(aCookie)
用以上任一方法都可以生成一個(gè)有“userName”項(xiàng)的文件, 在你的Internet臨時(shí)文件夾中你可以查看它。
也可以創(chuàng)建和添加有子鍵的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()方法輸出它到頁(yè)面,如:
Response.Write(Server.HtmlEncode(Request.Cookies["userInfo"]["userName"]));
或賦值給其它變量:
string strCookie1=Server.HtmlEncode(Request.Cookies["userInfo"]["userName"]);
用Cookies[i]數(shù)組可以檢索所有項(xiàng)和子鍵,如:
復(fù)制代碼 代碼如下:
string[] cooName = new string[Request.Cookies.Count];
string[] cooValue = new string[Request.Cookies.Count];
HttpCookie aCookie;
for(int i=0;i<Request.Cookies.Count;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<aCookie.Values.Count;j++){
subcooName[j] = Server.HtmlEncode(aCookie.Values.AllKeys[j]);
subcooValue[j] = Server.HtmlEncode(aCookie.Values[j]);
}
}
}
三、修改Cookies
如果是數(shù)值類型的Cookie值,比如訪問次數(shù),你可以讀取該值進(jìn)行加減操作后再存回,一般的修改直接存入新值就可以了,系統(tǒng)自動(dòng)用新值覆蓋原值,存入的方法與創(chuàng)建相同。
四、刪除Cookies
刪除Cookies只要把有效期設(shè)為失效就可以了,如在創(chuàng)建時(shí)設(shè)有效期為一天:
cookie.Expires = DateTime.Now.AddDays(1) ;
要?jiǎng)h除則設(shè)為:
cookie.Expires = DateTime.Now.AddDays(-1) ;
刪除子鍵:
復(fù)制代碼 代碼如下:
HttpCookie cookie;
cookie = Request.Cookies["userInfo"];
aCookie.Values.Remove("userName");
aCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(aCookie);
您可能感興趣的文章:
- C#基于cookie實(shí)現(xiàn)的購(gòu)物車功能
- C#基于WebBrowser獲取cookie的實(shí)現(xiàn)方法
- C#中Cookie之存儲(chǔ)對(duì)象
- C#自定義簡(jiǎn)化cookie類實(shí)例
- C#中Request.Cookies 和 Response.Cookies 的區(qū)別分析
- c#和net存取cookies操作示例
- C# javascript 讀寫Cookie的方法
- C#中的cookie編程簡(jiǎn)單實(shí)例與說明
- c# 獲取CookieContainer的所有cookies函數(shù)代碼
- 基于C#后臺(tái)調(diào)用跨域MVC服務(wù)及帶Cookie驗(yàn)證的實(shí)現(xiàn)
- c# 對(duì)cookies(增、刪、改、查)的操作方法
- C# HttpClient Cookie驗(yàn)證解決方法
- asp.net(C#)跨域及跨域?qū)慍ookie問題
- 總結(jié)C#網(wǎng)絡(luò)編程中對(duì)于Cookie的設(shè)定要點(diǎn)
相關(guān)文章
.net core使用FastHttpApi構(gòu)建web聊天室實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于.net core使用FastHttpApi構(gòu)建web聊天室的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10ASP.NET頁(yè)面某些選項(xiàng)進(jìn)行提示判斷具體實(shí)現(xiàn)
針對(duì)asp.net頁(yè)面某些選項(xiàng)進(jìn)行提示判斷,比如當(dāng)保存一個(gè)信息時(shí)候,需要對(duì)當(dāng)前信息是否為空進(jìn)行判斷2013-06-06WinForm中窗體間的數(shù)據(jù)傳遞交互的一些方法
通過子窗口向外引發(fā)一個(gè)事件,父窗口去實(shí)現(xiàn)該事件,我們可以再不關(guān)閉父窗口和子窗口的情況下進(jìn)行數(shù)據(jù)的傳輸顯示2012-12-12.Net彈性和瞬態(tài)故障處理庫(kù)Polly實(shí)現(xiàn)執(zhí)行策略
這篇文章介紹了.Net彈性和瞬態(tài)故障處理庫(kù)Polly實(shí)現(xiàn)執(zhí)行策略的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06.Net Core內(nèi)存回收模式及性能測(cè)試對(duì)比分析
下面小編就為大家分享一篇.Net Core內(nèi)存回收模式及性能測(cè)試對(duì)比分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12