#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. (vue.js)import "mint-ui/lib/stylecss"失败

    在vue2.0中引入了mint-ui后总是报一个css的错误 但是package.json中已经配置了css-loader style-loader ,webpack.config中也已经配置了css ...

  2. SpringBoot dubbo之class is not visible from class loader

    使用了两个类加载器加载了同一个类,区分一个Class对象是否相等要看包名+类名,也要看是否是同一个类加载器 方案一,排查掉spring-boot-devtools模块的maven引入可以解决,这时候所 ...

  3. P4196 [CQOI2006]凸多边形 半平面交

    \(\color{#0066ff}{题目描述}\) 逆时针给出n个凸多边形的顶点坐标,求它们交的面积.例如n=2时,两个凸多边形如下图: 则相交部分的面积为5.233. \(\color{#0066f ...

  4. P2115 [USACO14MAR]破坏Sabotage

    题意:给你一个正整数序列,让你删去一段区间内的数[l,r] $1<l\le r <n$ 使得剩余的数平均值最小$n\le 10^5$ 1.不难想到暴力,用前缀和优化$O(n^2)$ #in ...

  5. 最短路【洛谷P3946】ことりのおやつ(小鸟的点心)

    P3946 ことりのおやつ(小鸟的点心) 滑完雪之后,ことり突然想吃点心啦!于是她去了甜品店. 日本的冬天经常下雪.不幸的是,今天也是这样,每秒钟雪的厚度会增加q毫米. 秋叶原共有n个地点,编号从1到 ...

  6. spring中使用缓存

    一.启用对缓存的支持 Spring 对缓存的支持最简单的方式就是在方法上添加@Cacheable和@CacheEvict注解, 再添加注解之前,必须先启用spring对注解驱动的支持,基于java的配 ...

  7. git 创建、切换和提交新分支

    查看本地分支 git branch 创建新的分支 git branch <newBranch> 切换分支 git checkout <branchName> 创建并切换分支 g ...

  8. AtCoder - 2581 树状数组

    You are given an integer sequence of length N, a= {a1,a2,…,aN}, and an integer K. a has N(N+1)⁄2 non ...

  9. kuangbin专题十六 KMP&&扩展KMP HDU4300 Clairewd’s message

    Clairewd is a member of FBI. After several years concealing in BUPT, she intercepted some important ...

  10. 洛谷 P1036 选数

    嗯.... 这种类型的题在新手村出现还是比较正常的, 但是不知道为什么它的分类竟然是过程函数与递归!!!(难道这不是一个深搜题吗??? 好吧这就是一道深搜题,所以千万别被误导... 先看一下题目: 题 ...