#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. c++滚动数组

    说来惭愧,我老早以前就学习了dp,可直到最近才知道滚动数组. 所以说,滚动数组是什么呢? 它是一种优化dp空间复杂度的思想. 在dp转移时,我们往往不需要之前推的所有的,而是只需要前一两个转移的. 我 ...

  2. B:魔兽世界之一:备战

    描述 魔兽世界的西面是红魔军的司令部,东面是蓝魔军的司令部.两个司令部之间是依次排列的若干城市. 红司令部,City 1,City 2,……,City n,蓝司令部 两军的司令部都会制造武士.武士一共 ...

  3. C++期中考试

    第一题1. 补足日期类实现,使得能够根据日期获取这是一年中第多少天.(12分) date.h #ifndef DATE_H #define DATE_H class Date { public: Da ...

  4. ADX3000二层的负载均衡设计问题

    我的想法是 想在现有的局域网内部,利用ADX划分出一个新的局域网,模拟负载均衡. 现在有三台试验机器,拓扑图如下: 各个机器IP设置如下图: 我进行了如下的操作: 1 在组网配置当中,设置eth1_0 ...

  5. luogu3380 树套树之线段树套线段树

    个人感觉可能是最不需要脑子写的方法 不过也不太好调 就是用一个普通的线段树维护这个序列,但是对于线段树的每一个区间,再开一个动态开点的权值线段树,里面存储这个区间所有元素值 单点修改只会涉及到log棵 ...

  6. An error has occurred. See error log for more details. java.lang.NullPointerException

    今天重新新建一个eclipse的空间,就报了这么一个错误,百度找了很多,发现不对头,都不行.后来点开错误一看,原来是web.xml里面没有文件头造成的. 解决办法:只需要在web.xml文件里添加文件 ...

  7. C++基础学习2:命名空间

    C++语言引入命名空间(Namespace)这一概念主要是为了避免命名冲突,其关键字为 namespace. 科技发展到如今,一个系统通常都不会仅由一个人来开发完成,不同的人开发同一个系统,不可避免地 ...

  8. Jenkins 相关

    手动下载Jenkins plugin 的地址, 下载后的是zip 文件,然后再手动修改为.hpi 文件,然后再手动上传 https://plugins.jenkins.io/

  9. 处理Json接收日期格式问题

    //处理Json接收日期格式问题function getNormalDate(dateNormal) { var receiveDate = new Date(parseInt(dateNormal. ...

  10. 实验吧之Canon

    解压zip文件得到一个mp3文件和一个zip压缩包,解压需要密码,那密码就在mp3里面,使用MO3Stego好像不能解析出文本,说明解析需要密码,此时通过网上的讨论说标题Canon就是密码,就试着用了 ...