最近项目中用到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. Multiple “order by” in LINQ(转载)

    问: I have two tables, movies and categories, and I get an ordered list by categoryID first and then ...

  2. UOJ219 NOI2016 优秀的拆分 二分、字符串哈希

    传送门 题目可以转化为求\(AA\)的数量,设\(cnt1_x\)表示左端点为\(x\)的\(AA\)的数量,\(cnt2_x\)表示右端点为\(x\)的\(AA\)的数量,那么答案就是\(\sum ...

  3. 解决Oracle登录极慢的问题

    原文首发 http://anforen.com/wp/2018/04/oracle_login_slowly/ Oracle用PL/SQL登录,特别慢,3分钟以上,如果以前正常,并且按常见问题排查过, ...

  4. 使用Win PE修改其他硬盘中的系统注册表

    使用场景:原来装的机械硬盘系统盘为C盘,后来买了个SSD固态硬盘后,进入WinPE系统后,把原来的C盘整个复制到了固态硬盘,然后用BooticeX64.exe工具在UEFI启动中增加SSD固态硬盘中的 ...

  5. 手机端@media的屏幕适配

    @media only screen and (width: 320px) { html { font-size: 16px; }} @media only screen and (width: 36 ...

  6. Python高级特性(切片,迭代,列表生成式,生成器,迭代器)

    掌握了Python的数据类型.语句和函数,基本上就可以编写出很多有用的程序了. 比如构造一个1, 3, 5, 7, ..., 99的列表,可以通过循环实现: L = [] n = 1 while n ...

  7. Luogu P4317 花神的数论题

    也是一道不错的数位DP,考虑先转成二进制后再做 转化一下问题,考虑统计出\([1,n]\)中在二进制下有\(i\)个\(1\)的方案数\(cnt_i\),那么答案显然就是\(\prod i^{cnt_ ...

  8. Tensorflow实例:利用LSTM预测股票每日最高价(一)

    RNN与LSTM 这一部分主要涉及循环神经网络的理论,讲的可能会比较简略. 什么是RNN RNN全称循环神经网络(Recurrent Neural Networks),是用来处理序列数据的.在传统的神 ...

  9. VS2015 搭建 Asp.net core 开发环境

    1.首先你得装个vs2015 并且保证已经升级至 update3及以上(此处附上一个vs2015带up3的下载链接: ed2k://|file|cn_visual_studio_enterprise_ ...

  10. 案例学Python--案例四:Django实现一个网站的雏形(2)

    续上篇,用Django创建了一个Web,我们肯定想展示自己的页面,简单点,我们想看到自己的HelloWorld.此处要从项目的配置说起,方法和路径配对了,展现页面分分钟的事情. 先上效果图吧:     ...