cookies的创建:
在客户端创建一个username的cookies,其值为oneday,有效期为1天.
方法1:
方法2:
cookies的读取:
无子键读取:
有子键读取:
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.setcookie("name", server.urlencode("aaa"),1),读取时解码即可
另外:只要不给cookie设置过期时间,cookie在浏览器关闭的时候自动失效
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.setcookie("name", server.urlencode("aaa"),1),读取时解码即可
另外:只要不给cookie设置过期时间,cookie在浏览器关闭的时候自动失效
如对本文有疑问, 点击进行留言回复!!
使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)
C#实现获取本地内网(局域网)和外网(公网)IP地址的方法分析
网友评论