读取和写入Cookies
#region 读取或写入cookie
2 /// <summary>
3 /// 写cookie值
4 /// </summary>
5 /// <param name="strName">名称</param>
6 /// <param name="strValue">值</param>
7 public static void WriteCookie(string strName, string strValue)
8 {
9 HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
10 if (cookie == null)
11 {
12 cookie = new HttpCookie(strName);
13 }
14 cookie.Value = UrlEncode(strValue);
15 HttpContext.Current.Response.AppendCookie(cookie);
16 }
17
18 /// <summary>
19 /// 写cookie值
20 /// </summary>
21 /// <param name="strName">名称</param>
22 /// <param name="strValue">值</param>
23 public static void WriteCookie(string strName, string key, string strValue)
24 {
25 HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
26 if (cookie == null)
27 {
28 cookie = new HttpCookie(strName);
29 }
30 cookie[key] = UrlEncode(strValue);
31 HttpContext.Current.Response.AppendCookie(cookie);
32 }
33
34 /// <summary>
35 /// 写cookie值
36 /// </summary>
37 /// <param name="strName">名称</param>
38 /// <param name="strValue">值</param>
39 public static void WriteCookie(string strName, string key, string strValue, int expires)
40 {
41 HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
42 if (cookie == null)
43 {
44 cookie = new HttpCookie(strName);
45 }
46 cookie[key] = UrlEncode(strValue);
47 cookie.Expires = DateTime.Now.AddMinutes(expires);
48 HttpContext.Current.Response.AppendCookie(cookie);
49 }
50
51 /// <summary>
52 /// 写cookie值
53 /// </summary>
54 /// <param name="strName">名称</param>
55 /// <param name="strValue">值</param>
56 /// <param name="strValue">过期时间(分钟)</param>
57 public static void WriteCookie(string strName, string strValue, int expires)
58 {
59 HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
60 if (cookie == null)
61 {
62 cookie = new HttpCookie(strName);
63 }
64 cookie.Value = UrlEncode(strValue);
65 cookie.Expires = DateTime.Now.AddMinutes(expires);
66 HttpContext.Current.Response.AppendCookie(cookie);
67 }
68 /// <summary>
69 /// 写cookie值
70 /// </summary>
71 /// <param name="strName">名称</param>
72 /// <param name="expires">过期时间(天)</param>
73 public static void WriteCookie(string strName, int expires)
74 {
75 HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
76 if (cookie == null)
77 {
78 cookie = new HttpCookie(strName);
79 }
80 cookie.Expires = DateTime.Now.AddDays(expires);
81 HttpContext.Current.Response.AppendCookie(cookie);
82 }
83
84 /// <summary>
85 /// 写入COOKIE,并指定过期时间
86 /// </summary>
87 /// <param name="strName">KEY</param>
88 /// <param name="strValue">VALUE</param>
89 /// <param name="expires">过期时间</param>
90 public static void iWriteCookie(string strName, string strValue, int expires)
91 {
92 HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
93 if (cookie == null)
94 {
95 cookie = new HttpCookie(strName);
96 }
97 cookie.Value = strValue;
98 if (expires > 0)
99 {
100 cookie.Expires = DateTime.Now.AddMinutes((double)expires);
101 }
102 HttpContext.Current.Response.AppendCookie(cookie);
103 }
104
105 /// <summary>
106 /// 读cookie值
107 /// </summary>
108 /// <param name="strName">名称</param>
109 /// <returns>cookie值</returns>
110 public static string GetCookie(string strName)
111 {
112 if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
113 return UrlDecode(HttpContext.Current.Request.Cookies[strName].Value.ToString());
114 return "";
115 }
116
117 /// <summary>
118 /// 读cookie值
119 /// </summary>
120 /// <param name="strName">名称</param>
121 /// <returns>cookie值</returns>
122 public static string GetCookie(string strName, string key)
123 {
124 if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null && HttpContext.Current.Request.Cookies[strName][key] != null)
125 return UrlDecode(HttpContext.Current.Request.Cookies[strName][key].ToString());
126
127 return "";
128 }
129 #endregion
读取和写入Cookies的更多相关文章
- ios htttp网络请求cookie的读取与写入(NSHTTPCookieStorage)
当你访问一个网站时,NSURLRequest都会帮你主动记录下来你访问的站点设置的Cookie,如果 Cookie 存在的话,会把这些信息放在 NSHTTPCookieStorage 容器中共享,当你 ...
- JavaScript之读取和写入cookie
首先先让我们简单地了解一下cookie. 在我们制作网页的过程中,经常需要把信息从一个页面传递给另一个页面,这时候就需要用到JavaScript中的cookie机制了.简单说来,cookie提 ...
- asp.net报错“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”的解决办法
来源:http://ajxfxb.blog.163.com/blog/static/56675086201411634336878/ 作者是:没完没了的工作 asp.net报错“尝试读取或写入受保护的 ...
- SQLBulkCopy使用实例--读取Excel写入数据库/将 Excel 文件转成 DataTable
MS SQL Server 提供一个称为 bcp 的流行的命令提示符实用工具,用于将数据从一个表移动到另一个表(表可以在不同服务器上). SqlBulkCopy 类允许编写提供类似功能的托管代码解决方 ...
- SQLServer2012在登录远程服务器实例时报错:尝试读取或写入受保护的内存
SQLServer2012在登录远程服务器实例时报错:尝试读取或写入受保护的内存.这通常指示其它内存已损坏.(System.Data). 而登录本地数据库实例则能顺利登入,不存在上述问题. 试一试重置 ...
- 如何通过SerialPort读取和写入设备COM端口数据
SerialPort类用于控制串行端口文件资源.提供同步 I/O 和事件驱动的 I/O.对管脚和中断状态的访问以及对串行驱动程序属性的访问.另外,SerialPort的功能可以包装在内部 Stream ...
- properties配置文件的读取和写入
/** * 类名:PropertiesUtil * 功能:提供对properties配置文件的读取和写入 * @author ChengTao */package com.xy.xyd.rest.bi ...
- 读取和写入 文件 (NSFIleManger 与 NSFileHandle)
读取和写入 文件 //传递文件路径方法 -(id)initPath:(NSString *)srcPath targetPath:(NSString *)targetPath { self = [su ...
- AccessViolationException: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
在使用SqlConnection.Open()连接到SQL Server 2008 R2 SP2时,一直报以下异常: AccessViolationException: 尝试读取或写入受保护的内存.这 ...
随机推荐
- NSNumber数字
前言 将基本数据类型包装成 OC 对象 1.NSNumber 与 基本数据类型 的相互转换 // 基本数据类型 转 NSNumber // 对象方法,将整形数据转换为 OC 对象 NSNumber * ...
- HDU - 1754 I Hate It (线段树单点修改,求区间最大值)
很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老师有 ...
- 区块链中的密码学(-)区块链中运用最广的散列算法-SHA256算法分析与实现
在很多技术人员的眼中,区块链并不是一种新的技术,而是过去很多年计算机技术的组合运用.而在这个方方面面技术的运用上,基于密码学的加密算法可以说是区块链各种特点得以表现的根本,一旦目前使用的加密算法被证实 ...
- Swift 4.0 正式发布,更快更兼容更好用
Swift4现已正式发布!Swift4在Swift3的基础上,提供了更强大的稳健性和稳定性,为Swift3提供源码兼容性,对标准库进行改进,并添加了归档和序列化等功能. 你可以通过观看WWDC2017 ...
- Set\HashSet集合为什么能去重(转)
如果想查找一个集合中是否包含有某个对象,大概的程序代码怎样写呢?当发现某个元素与要查找的对对象进行equals方法比较的结果相等时,则停止继续查找并返回肯定的信息,否则返回否定的信息.如果是一个集合中 ...
- POJ2528 Mayor's posters(线段树+离散化)
题意 : 在墙上贴海报, n(n<=10000)个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000).求出最后还能看见多少张海报. 分析 ...
- 解决mac下ssh空闲一段时间自动断开的问题
之前在公司就遇到过这种问题,使用ssh登录linux服务器后,在后台放置一段时间,会发现会自动断开,解决的方法很简单: vim /etc/ssh/ssh_config 添加这2句即可 ServerAl ...
- A. Free Cash
A. Free Cash time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Django ORM 最后操作
F查询:代表2个字段之间的比较 from django.db.models import F models.Book.objects.filter(price__gt=F('keep_price')) ...
- logback日志简记
%date{HH:mm:ss.SSS} [%thread] %-5level %logger{20}:%line - %msg%n 输出: 09:54:09.657 [main] INFO c.e. ...