#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的更多相关文章

  1. ios htttp网络请求cookie的读取与写入(NSHTTPCookieStorage)

    当你访问一个网站时,NSURLRequest都会帮你主动记录下来你访问的站点设置的Cookie,如果 Cookie 存在的话,会把这些信息放在 NSHTTPCookieStorage 容器中共享,当你 ...

  2. JavaScript之读取和写入cookie

      首先先让我们简单地了解一下cookie.   在我们制作网页的过程中,经常需要把信息从一个页面传递给另一个页面,这时候就需要用到JavaScript中的cookie机制了.简单说来,cookie提 ...

  3. asp.net报错“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”的解决办法

    来源:http://ajxfxb.blog.163.com/blog/static/56675086201411634336878/ 作者是:没完没了的工作 asp.net报错“尝试读取或写入受保护的 ...

  4. SQLBulkCopy使用实例--读取Excel写入数据库/将 Excel 文件转成 DataTable

    MS SQL Server 提供一个称为 bcp 的流行的命令提示符实用工具,用于将数据从一个表移动到另一个表(表可以在不同服务器上). SqlBulkCopy 类允许编写提供类似功能的托管代码解决方 ...

  5. SQLServer2012在登录远程服务器实例时报错:尝试读取或写入受保护的内存

    SQLServer2012在登录远程服务器实例时报错:尝试读取或写入受保护的内存.这通常指示其它内存已损坏.(System.Data). 而登录本地数据库实例则能顺利登入,不存在上述问题. 试一试重置 ...

  6. 如何通过SerialPort读取和写入设备COM端口数据

    SerialPort类用于控制串行端口文件资源.提供同步 I/O 和事件驱动的 I/O.对管脚和中断状态的访问以及对串行驱动程序属性的访问.另外,SerialPort的功能可以包装在内部 Stream ...

  7. properties配置文件的读取和写入

    /** * 类名:PropertiesUtil * 功能:提供对properties配置文件的读取和写入 * @author ChengTao */package com.xy.xyd.rest.bi ...

  8. 读取和写入 文件 (NSFIleManger 与 NSFileHandle)

    读取和写入 文件 //传递文件路径方法 -(id)initPath:(NSString *)srcPath targetPath:(NSString *)targetPath { self = [su ...

  9. AccessViolationException: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。

    在使用SqlConnection.Open()连接到SQL Server 2008 R2 SP2时,一直报以下异常: AccessViolationException: 尝试读取或写入受保护的内存.这 ...

随机推荐

  1. 阿里云服务器 ECS 部署lamp:centos+apache+mysql+php安装配置方法 (centos7)

    阿里云服务器 ECS 部署lamp:centos+apache+mysql+php安装配置方法 (centos7) 1.效果图 1 2. 部署步骤 1 1. mysql安装附加(centos7) 7 ...

  2. 洛谷P4462 [CQOI2018]异或序列(莫队)

    打广告->[这里](https://www.cnblogs.com/bztMinamoto/p/9538115.html) 我蠢了…… 如果$a_{l} xor ...a_{r}=k$,那么只要 ...

  3. Gym - 101845D 计算几何

    Ignacio is a famous mathematician, some time ago he was married with Dolly, a famous scientific, at ...

  4. Session_Start

    第一次启动浏览器后,执行了包含Session_Start的方法时触发,当关闭浏览器或者SESSION超时后,自动销毁.

  5. 牛客寒假算法基础集训营5 J 炫酷数学

    链接:https://ac.nowcoder.com/acm/contest/331/J来源:牛客网 小希最近想知道一个东西,就是A+B=A|B(其中|为按位或)的二元组有多少个. 当然,直接做这个式 ...

  6. ribbon负载均衡

    ribbon的负载均衡策略有很多 IRule 这是所有负载均衡策略的父接口,里边的核心方法就是choose方法,用来选择一个服务实例. AbstractLoadBalancerRule Abstrac ...

  7. HIVE锁相关

    hive存在两种锁,共享锁Shared (S)和互斥锁Exclusive (X) 其中只触发s锁的操作可以并发的执行,只要有一个操作对表或者分区出发了x锁,则该表或者分区不能并发的执行作业. -- 加 ...

  8. 练习五十七:for循环 809??=800*?+9*?+1其中?代表的两位数,8*?的结果为两位数,9*?的结果为3位数。求?代表的两位数,及809??后的结果

    题目:809??=800*?+9*?+1其中?代表的两位数,8*?的结果为两位数,9*?的结果为3位数.求?代表的两位数,及809??后的结果 注意:一定要看清楚题目哦,809??代表的是结果,?代表 ...

  9. Problem08 输入数字求和

    题目:求s=a+aa+aaa+aaaa+aa...a 的值,其中a 是一个数字.例如2+22+222+2222+22222(此时共有5 个数相加),几个数相加有键盘控制. 程序分析:关键是计算出每一项 ...

  10. my24_mysql索引-使用篇

    索引提示 SELECT * FROM table1 USE INDEX (col1_index,col2_index) ; SELECT * FROM table1 IGNORE INDEX (col ...