博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
提供一个Cookies操作类(支持带域名与不带域名)IP和localhost域名调试
阅读量:7235 次
发布时间:2019-06-29

本文共 5488 字,大约阅读时间需要 18 分钟。

来看一下COOKIES公用操作类

 
/// 
/// Cookie 操作帮助类
/// 
public class CookieHelper
{
 
#region 写cookie值
/// 
/// 写cookie值
/// 
/// 名称
/// 
public static void Write(string strName, string strValue)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
cookie = new HttpCookie(strName);
}
cookie.Value = strValue;
HttpContext.Current.Response.AppendCookie(cookie);
 
}
/// 
/// 写cookie值
/// 
/// 名称
/// 
/// 域  例如:contoso.com
public static void WriteWithDomain(string strName, string strValue, string doMain)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
cookie = new HttpCookie(strName);
}
cookie.Value = strValue;
cookie.Domain = doMain;
HttpContext.Current.Response.AppendCookie(cookie);
 
}
 
/// 
/// 写cookie值
/// 
/// 名称
/// 键名
/// 
public static void Write(string strName, string key, string strValue)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
cookie = new HttpCookie(strName);
}
cookie[key] = strValue;
HttpContext.Current.Response.AppendCookie(cookie);
 
}
/// 
/// 写cookie值
/// 
/// 名称
/// 键名
/// 
/// 域  例如:contoso.com
public static void WriteWithDomain(string strName, string key, string strValue
, string doMain)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
cookie = new HttpCookie(strName);
}
cookie.Domain = doMain;
cookie[key] = strValue;
HttpContext.Current.Response.AppendCookie(cookie);
 
}
 
/// 
/// 写cookie值
/// 
/// 名称
/// 
/// 过期时间(分钟)
public static void Write(string strName, string strValue, int expires)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
cookie = new HttpCookie(strName);
}
 
cookie.Value = strValue;
cookie.Expires = System.DateTime.Now.AddMinutes(expires);
HttpContext.Current.Response.AppendCookie(cookie);
}
/// 
/// 写cookie值
/// 
/// 名称
/// 
/// 过期时间(分钟)
/// 域  例如:contoso.com
public static void WriteWithDomain(string strName, string strValue, int expires
, string doMain)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
cookie = new HttpCookie(strName);
}
cookie.Domain = doMain;
cookie.Value = strValue;
cookie.Expires = System.DateTime.Now.AddMinutes(expires);
HttpContext.Current.Response.AppendCookie(cookie);
}
/// 
/// 写cookie值
/// 
/// 名称
/// 键名
/// 
/// 过期时间(分钟)
public static void Write(string strName, string key, string strValue, int expires)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
cookie = new HttpCookie(strName);
}
cookie[key] = strValue;
cookie.Expires = System.DateTime.Now.AddMinutes(expires);
HttpContext.Current.Response.AppendCookie(cookie);
}
/// 
/// 写cookie值
/// 
/// 名称
/// 键名
/// 
/// 过期时间(分钟)
/// 域  例如:contoso.com
public static void WriteWithDomain(string strName, string key, string strValue
, int expires, string doMain)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
cookie = new HttpCookie(strName);
}
cookie.Domain = doMain;
cookie[key] = strValue;
cookie.Expires = System.DateTime.Now.AddMinutes(expires);
HttpContext.Current.Response.AppendCookie(cookie);
}
 
#endregion
 
#region 读cookie值
 
/// 
/// 读cookie值
/// 
/// 名称
/// 
cookie值
public static string Read(string strName)
{
if (HttpContext.Current.Request.Cookies != null
&& HttpContext.Current.Request.Cookies[strName] != null)
{
return HttpContext.Current.Request.Cookies[strName].Value;
}
else
{
return string.Empty;
}
}
 
/// 
/// 读cookie值
/// 
/// 名称
/// 键名
/// 
cookie值
public static string Read(string strName, string key)
{
if (HttpContext.Current.Request.Cookies != null
&& HttpContext.Current.Request.Cookies[strName] != null)
{
return HttpContext.Current.Request.Cookies[strName][key];
}
else
{
return string.Empty;
}
}
 
#endregion
 
#region Cookie 删除
/// 
/// 删除
/// 
/// 名称
public static void Remove(string name)
{
if (HttpContext.Current.Request.Cookies != null
&& HttpContext.Current.Request.Cookies[name] != null)
{
HttpCookie myCookie = new HttpCookie(name);
myCookie.Expires = DateTime.Now.AddMinutes(-1);
HttpContext.Current.Response.Cookies.Add(myCookie);
}
}
 
/// 
/// 删除
/// 
/// 名称
/// 二级建名称
public static void Remove(string name, string key)
{
if (HttpContext.Current.Request.Cookies != null
&& HttpContext.Current.Request.Cookies[name] != null
&& !string.IsNullOrEmpty(HttpContext.Current.Request.Cookies[name][key]))
{
 

string[] temp = HttpContext.Current.Request.Cookies[name].Value

.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);

List
list = new List
();
foreach (string item in temp)
{
if (item.StartsWith(key))
{
continue;
}
else
{
list.Add(item);
}
}
Write(name, string.Join("&", list.ToArray()));
}
}
#endregion
 
 
}

而我们通过这个公用类来对cookies操作就显得容易了许多

public void Write()
{
//不用域名,通用性
VCommons.Http.CookieHelper.Write("test", "name", "bobo123");
//加域名,IP地址形式
VCommons.Http.CookieHelper.WriteWithDomain("test2", "nameByDomain"
, "bobo>Domain>IP", "127.0.0.1");
//加域名,可以用localhost进行本地调试
VCommons.Http.CookieHelper.WriteWithDomain("test3", "nameByDomain"
, "bobo>localhost", "localhost");
 
}
public void Clear()
{
 
//清除cookies名称为test的所有cookies元素
VCommons.Http.CookieHelper.Remove("test");
 

//将指定名称下的指定键值设置成空

VCommons.Http.CookieHelper.Write("test", "name", string.Empty);

VCommons.Http.CookieHelper.Write("test2", "nameByDomain", string.Empty);
VCommons.Http.CookieHelper.Write("test3", "nameByDomain", string.Empty);
 
}

转载地址:http://vpofm.baihongyu.com/

你可能感兴趣的文章
虚拟互换(virtual swap)
查看>>
点滴记录——在Ubuntu 14.04中使SublimeText 3支持中文输入法
查看>>
ARP欺骗病毒,网页“篡改”,注入iframe代码!
查看>>
IE读取并显示本地图像文件的方法
查看>>
自学android半年,已从.net转型成android程序员,分享下这个过程
查看>>
ImageView显示网络图片
查看>>
linux防止sshd被爆破(安装denyhosts)
查看>>
【P4语言学习】Parser解析器
查看>>
python问题:AttributeError: 'module' object has no attribute 'SSL_ST_INIT'(转)
查看>>
测试人员职业规划
查看>>
Twenty Newsgroups Classification任务之二seq2sparse(3)
查看>>
教育单元测试mock框架优化之路(中)
查看>>
网易老司机:网络性能问题该怎样监控与定位?
查看>>
为什么LinkedIn放弃MySQL slowlog,转用基于网络层的慢查询分析器?
查看>>
何小鹏:跟李斌赌输了 将付一辆ES8顶配的购车款给对方
查看>>
OPPO R11继续升温,8月8日将推出巴萨定制机
查看>>
从主流厂商宣传语看中国CRM领域的发展
查看>>
腾讯动漫回应停止支付作者稿酬传闻:均会按时如数支付
查看>>
卫健委:保障病人就医需求 加强流感药品物资保障
查看>>
因无力偿还网贷 内蒙古一对夫妻贩卖亲生龙凤胎
查看>>