最近项目中用到cookie,所以就写了个cookie帮助类
public class CookHelper
{
HttpResponseBase Response = null;
HttpRequestBase Request = null;
public CookHelper(HttpResponseBase Response, HttpRequestBase Request)
{
this.Response = Response;
this.Request = Request;
} /// <summary>
/// 设置cookie
/// </summary>
/// <param name="key">key</param>
/// <param name="value">value</param>
/// <param name="expiredTime">过期时间</param>
public void SetCookie(string key, string value,int expiredTime=7)
{
Response.Cookies[key].Value = value;
Response.Cookies[key].Expires = DateTime.Now.AddDays(expiredTime); //测试
//Response.Cookies[key].Expires = DateTime.Now.AddMinutes(2);
} /// <summary>
/// 获取cookie
/// </summary>
/// <param name="key">key</param>
/// <returns></returns>
public string GetCookie(string key)
{
if (Request.Cookies[key] != null)
{
return Request.Cookies[key].Value;
}
else
{
return null;
}
} /// <summary>
/// 删除cookie
/// </summary>
/// <param name="key">key</param>
public void RemoveCookie(string key)
{
foreach (string cookiename in Request.Cookies.AllKeys)
{
if (cookiename == key)
{
HttpCookie cookies = Request.Cookies[cookiename];
if (cookies != null)
{
cookies.Expires = DateTime.Today.AddDays(-1);
Response.Cookies.Add(cookies);
Request.Cookies.Remove(cookiename);
}
}
}
}
}

Cookie 类的更多相关文章

  1. Servlet--HttpSession接口,HttpSessionContext接口,Cookie类

    HttpSession接口 定义 public interface HttpSession 这个接口被 Servlet 引擎用来实现在 HTTP 客户端和 HTTP 会话两者的关联.这种关联可能在多外 ...

  2. Cookie类

    Cookie类:用于存储会话数据 1)构造Cookie对象 Cookie(java.lang.String name, java.lang.String value) 2)设置cookie void ...

  3. C# 操作Cookie类

    1.Cookie操作类 using System; using System.Data; using System.Configuration;using System.Web;using Syste ...

  4. asp.net操作cookie类,包含datatable批量存入cookie

    以下是类: public class CookieMgr { #region 快速储存Cookie /// <summary> /// 快速储存Cookie /// </summar ...

  5. asp.net操作cookie类

    using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary ...

  6. PHP会话cookie类的封装

    <?php header('content-type:text/html;charset=utf-8');/** * 完成cookie的设置.删除.更新.读取 */class Cookie{   ...

  7. Cookie操作类、压缩、序列化

    1.cookie类 CartCookie.cs using System; using System.Data; using System.Configuration; using System.We ...

  8. Spring cookie 实战

    测试环境搭建 使用Springboot构建web server, 在测试方法中打印接收的cookie. @RestController @RequestMapping("/register/ ...

  9. [JavaEE笔记]Cookie

    引言 由于 Http 是一种无状态的协议,服务器单从网络连接上无从知道客户身份. 会话跟踪是 Web 程序中常用的技术,用来跟踪用户的整个会话.常用会话跟踪技术是 Cookie 与 Session. ...

随机推荐

  1. highcharts为X轴标签添加链接

    $(function () { var categoryLinks = { 'Foo': 'http://www.google.com/search?q=foo', 'Bar': 'http://ww ...

  2. (转)Linux的用户和用户组管理

    原文 Linux是个多用户多任务的分时操作系统,所有一个要使用系统资源的用户都必须先向系统管理员申请一个账号,然后以这个账号的身份进入系统.用户的账号一方面能帮助系统管理员对使用系统的用户进行跟踪,并 ...

  3. 【强化学习】python 实现 q-learning 迷宫通用模板

    本文作者:hhh5460 本文地址:https://www.cnblogs.com/hhh5460/p/10145797.html 0.说明 这里提供了二维迷宫问题的一个比较通用的模板,拿到后需要修改 ...

  4. .Net架构篇:思考如何设计一款实用的分布式监控系统?

    前言 无论从最早期的unix操作系统,还是曾经大行其道的单体式应用,还是现在日益流行的微服务架构,始终都离不开监控的身影.如windows的任务管理器,linux的top命令,都可以看作是监控的面板. ...

  5. element-ui + vue + node.js 与 服务器 Python 应用的跨域问题

    跨越问题解决的两种办法: 1. 在 config => index.js 中配置 proxyTable 代理: proxyTable: { '/charts': { target: 'http: ...

  6. xmlSpy套件(Altova MissionKit 2016)的Ollydbg调试过程及破解

    最近工作需要用到XML处理软件,网上找到Altova MissionKit 2016( 包含了XmlSpy.MapForce.StyleVision.UModel.DatabaseSpy等工具),用了 ...

  7. C. Multiplicity

    链接 [http://codeforces.com/contest/1061/problem/C] 题意 给你一个数组,让你找有多少个子串(并非连续,但相对位置不能换),满足bi%i==0; 分析 d ...

  8. 关于QQ的NABCD模型

    关于QQ的NABCD模型 N--Need 随着电脑的普及,人们在网络上进行交流的时间越来越多,由于现有的交流工具还不是那么的完善,还不能够完全满足人们在交流时的需求.因此为了满足人们更多的需求,我们设 ...

  9. 小学四则运算APP 最后阶段

    团队成员:陈淑筠.杨家安.陈曦 团队选题:小学四则运算APP 这次发布的是我们APP的最终版本!图片背景有根据用户需求改变!还增加了草稿纸运算的画布功能! 运行结果如下: package com.ex ...

  10. HttpServletResponse类学习

    /*//2) 乱码的解决. //设置服务器输出的编码为UTF-8---在BaseServlet处已经已经进行了设置 response.setCharacterEncoding("UTF-8& ...