欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#和net存取cookies操作示例

程序员文章站 2024-02-24 12:37:04
cookies的创建:在客户端创建一个username的cookies,其值为oneday,有效期为1天. 方法1:复制代码 代码如下:response.cookies[...

cookies的创建:

在客户端创建一个username的cookies,其值为oneday,有效期为1天.

方法1:

复制代码 代码如下:

response.cookies["username"].value="admin";
response.cookies["username"].expires=datetime.now.adddays(1);

方法2:

复制代码 代码如下:

system.web.httpcookie newcookie=new httpcookie("username");
newcookie.value="oneday";
newcookie.expires=datetime.now.adddays(1);
response.appendcookie(newcookie);


创建带有子键的cookies:

复制代码 代码如下:

system.web.httpcookie newcookie=new httpcookie("user");
newcookie.values["username"]="admin";
newcookie.values["password"]="admin";
newcookie.expires=datetime.now.adddays(1);
response.appendcookie(newcookie);

cookies的读取:

无子键读取:

复制代码 代码如下:

if(request.cookies["username"]!=null)
{
response.write(server.htmlencode(request.cookies["username"].value));
}

有子键读取:

复制代码 代码如下:

if(request.cookies["user"]!=null)
{
response.write(server.htmlencode(request.cookies["user"]["username"].value));
response.write(server.htmlencode(request.cookies["user"]["password"].value));

using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;


public class cookie
{
    /// <summary>
    /// cookies赋值
    /// </summary>
    /// <param name="strname">主键</param>
    /// <param name="strvalue">键值</param>
    /// <param name="strday">有效天数</param>
    /// <returns></returns>
    public bool setcookie(string strname, string strvalue, int strday)
    {
        try
        {
            httpcookie cookie = new httpcookie(strname);
            //cookie.domain = ".xxx.com";//当要跨域名访问的时候,给cookie指定域名即可,格式为.xxx.com
            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">主键</param>
    /// <returns></returns>
    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">主键</param>
    /// <returns></returns>
    public bool delcookie(string strname)
    {
        try
        {
            httpcookie cookie = new httpcookie(strname);
            //cookie.domain = ".xxx.com";//当要跨域名访问的时候,给cookie指定域名即可,格式为.xxx.com
            cookie.expires = datetime.now.adddays(-1);
            system.web.httpcontext.current.response.cookies.add(cookie);
            return true;
        }
        catch
        {
            return false;
        }
    }
}



示例:
复制代码 代码如下:

cookie cookie = new cookie();
cookie.setcookie("name", "aaa",1);//赋值
cookie.getcookie("name");//取值
cookie.delcookie("name");//删除

注意:当cookie存中文出现乱码,则在存放时给中文编码,如cookie.setcookie("name", server.urlencode("aaa"),1),读取时解码即可

另外:只要不给cookie设置过期时间,cookie在浏览器关闭的时候自动失效

复制代码 代码如下:

using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;

public class cookie
{
    /// <summary>
    /// cookies赋值
    /// </summary>
    /// <param name="strname">主键</param>
    /// <param name="strvalue">键值</param>
    /// <param name="strday">有效天数</param>
    /// <returns></returns>
    public bool setcookie(string strname, string strvalue, int strday)
    {
        try
        {
            httpcookie cookie = new httpcookie(strname);
            //cookie.domain = ".xxx.com";//当要跨域名访问的时候,给cookie指定域名即可,格式为.xxx.com
            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">主键</param>
    /// <returns></returns>
    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">主键</param>
    /// <returns></returns>
    public bool delcookie(string strname)
    {
        try
        {
            httpcookie cookie = new httpcookie(strname);
            //cookie.domain = ".xxx.com";//当要跨域名访问的时候,给cookie指定域名即可,格式为.xxx.com
            cookie.expires = datetime.now.adddays(-1);
            system.web.httpcontext.current.response.cookies.add(cookie);
            return true;
        }
        catch
        {
            return false;
        }
    }
}

示例:

复制代码 代码如下:

cookie cookie = new cookie();
cookie.setcookie("name", "aaa",1);//赋值
cookie.getcookie("name");//取值
cookie.delcookie("name");//删除

注意:当cookie存中文出现乱码,则在存放时给中文编码,如cookie.setcookie("name", server.urlencode("aaa"),1),读取时解码即可

另外:只要不给cookie设置过期时间,cookie在浏览器关闭的时候自动失效