#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. 一键结束port 5037占用

    输入cmd进入dos界面,进入android-sdk-windows\platform-tools目录,执行下面命令启动adb start-server出现下面错误* daemon not runni ...

  2. 最全PyCharm教程--for python

    PyCharm简介: PyCharm是由JetBrains打造的一款Python IDE,VS2010的重构插件Resharper就是出自JetBrains之手.   同时支持Google App E ...

  3. idea 面板介绍

    一.面板说明 IDEA面板的全貌如下图 二.菜单栏 下面会简单介绍下一些常用的部分菜单使用,如有疑问或补充欢迎留言. (1).File文件 1. New:新建一个工程 可以新建project,导入已存 ...

  4. (原创)D-query SPOJ - DQUERY(莫队)统计不同数的数量

    A - D-query Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a ...

  5. 【转】HTTP协议/IIS 原理及ASP.NET运行机制浅析【图解】

    源地址:http://blog.csdn.net/chelen_jak/article/details/50040537

  6. hdu1403(后缀数组模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1403 题意: 给出两个字符串, 求他们的最长公共子串 思路: 两个字符串的最长公共子串长度显然就是两个 ...

  7. sql_trace基本用法

    sql_trace是oracle提供的一个非常好的跟踪工具,主要用来检查数据库的异常情况,通过跟踪数据库的活动,找到有问题的语句. 一.概述:    SQL_TRACE是Oracle的一个非常强大的工 ...

  8. Codeforces Round #534 (Div. 2) D. Game with modulo 交互题

    先二分一个区间,再在区间里面二分即可: 可以仔细想想,想明白很有意思的: #include<iostream> #include<cstdio> #include<alg ...

  9. HIbernate | 简单对象嵌套实践

    src/entity/Course.java public class Course { private Integer courseNo; private String courseName; pu ...

  10. System.Collections.Generic.List<T> 与 System.Collections.ArrayList

    [推荐] System.Collections.Generic.List<T> [原因] 泛型集合类List<T>在操作值类型的集合时可以不进行 装箱/拆箱 处理. 使得性能较 ...