C# 操作COOKIES通用类[转]
using System;
using System.Web;
/// <summary>
/// Cookie 的摘要说明
/// </summary>
public class Cookie
{
/// <summary>
/// 创建Cookies
/// </summary>
/// <param name="strName">Cookie 主键</param>
/// <param name="strValue">Cookie 键值</param>
/// <param name="strDay">Cookie 天数</param>
/// <code>Cookie ck = new Cookie();</code>
/// <code>ck.setCookie("主键","键值","天数");</code>
public bool setCookie(string strName, string strValue, int strDay)
{
try
{
HttpCookie Cookie = new HttpCookie(strName);
Cookie.Expires = DateTime.Now.AddDays(strDay);
Cookie.Value = strValue;
System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 读取Cookies
/// </summary>
/// <param name="strName">Cookie 主键</param>
/// <code>Cookie ck = new Cookie();</code>
/// <code>ck.getCookie("主键");</code>
public string getCookie(string strName)
{
HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
if (Cookie != null)
{
return Cookie.Value.ToString();
}
else
{
return null;
}
}
/// <summary>
/// 删除Cookies
/// </summary>
/// <param name="strName">Cookie 主键</param>
/// <code>Cookie ck = new Cookie();</code>
/// <code>ck.delCookie("主键");</code>
public bool delCookie(string strName)
{
try
{
HttpCookie Cookie = new HttpCookie(strName);
Cookie.Expires = DateTime.Now.AddDays(-1);
System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
return true;
}
catch
{
return false;
}
}
}
using System.Web;
/// <summary>
/// Cookie 的摘要说明
/// </summary>
public class Cookie
{
/// <summary>
/// 创建Cookies
/// </summary>
/// <param name="strName">Cookie 主键</param>
/// <param name="strValue">Cookie 键值</param>
/// <param name="strDay">Cookie 天数</param>
/// <code>Cookie ck = new Cookie();</code>
/// <code>ck.setCookie("主键","键值","天数");</code>
public bool setCookie(string strName, string strValue, int strDay)
{
try
{
HttpCookie Cookie = new HttpCookie(strName);
Cookie.Expires = DateTime.Now.AddDays(strDay);
Cookie.Value = strValue;
System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 读取Cookies
/// </summary>
/// <param name="strName">Cookie 主键</param>
/// <code>Cookie ck = new Cookie();</code>
/// <code>ck.getCookie("主键");</code>
public string getCookie(string strName)
{
HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
if (Cookie != null)
{
return Cookie.Value.ToString();
}
else
{
return null;
}
}
/// <summary>
/// 删除Cookies
/// </summary>
/// <param name="strName">Cookie 主键</param>
/// <code>Cookie ck = new Cookie();</code>
/// <code>ck.delCookie("主键");</code>
public bool delCookie(string strName)
{
try
{
HttpCookie Cookie = new HttpCookie(strName);
Cookie.Expires = DateTime.Now.AddDays(-1);
System.Web.HttpContext.Current.Response.Cookies.Add(Cookie);
return true;
}
catch
{
return false;
}
}
}
满招损,谦受益,学,永无止境
posted on 2009-06-18 01:05 zeroStart 阅读(1018) 评论(0) 编辑 收藏 举报