设置: protected void Button1_Click(object sender, EventArgs e)
{
HttpCookie hc = new HttpCookie("test", "welcome to bj!");
hc.Domain = "localhost";
hc.Expires = DateTime.Now.AddMinutes(15);
Response.Cookies.Add(hc);
}
取值://另一页面
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("<script>alert('" +Request.Cookies["test"].Value + "');</script>");
}
{
HttpCookie hc = new HttpCookie("test", "welcome to bj!");
hc.Domain = "localhost";
hc.Expires = DateTime.Now.AddMinutes(15);
Response.Cookies.Add(hc);
}
取值://另一页面
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("<script>alert('" +Request.Cookies["test"].Value + "');</script>");
}
public static void SaveCookie(string CookieName, string CookieValue, double CookieTime)
{
HttpCookie myCookie = new HttpCookie(CookieName);
DateTime now = DateTime.Now;
myCookie.Value = HttpUtility.UrlEncode(CookieValue);//编码写入转换,防止中文乱码
if (CookieTime != 0)
{
myCookie.Expires = now.AddDays(CookieTime);
if (HttpContext.Current.Response.Cookies[CookieName] != null)
HttpContext.Current.Response.Cookies.Remove(CookieName);
HttpContext.Current.Response.Cookies.Add(myCookie);
}
else
{
if (HttpContext.Current.Response.Cookies[CookieName] != null)
HttpContext.Current.Response.Cookies.Remove(CookieName);
HttpContext.Current.Response.Cookies.Add(myCookie);
}
}
取Cookiepublic static string GetCookie(string CookieName)
{
HttpCookie myCookie = new HttpCookie(CookieName);
myCookie = HttpContext.Current.Request.Cookies[CookieName];
if (myCookie != null)
return HttpUtility.UrlDecode(myCookie.Value);//返回解码后的Cookie值
else
return null;
}
获取的时候
GetCookie("test");
http://www.cnblogs.com/shchdbk/archive/2010/04/21/1717449.html
function setCookie(name, value, days) {
if (days) {
var exp = new Date();
exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";domain=.cnonix.com";
} else {
document.cookie = name + "=" + escape(value);
}
}
function getCookie(name) {
var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
if (arr != null) return unescape(arr[2]); return null;
}http://www.cnonix.com/cnonixdb/list/0,在这个页面怎么取?
/// Cookies赋值
/// </summary>
/// <param name="strName">主键</param>
/// <param name="strValue">键值</param>
/// <param name="strDay">有效天数</param>
/// <returns></returns>
public static bool setCookie(string strName, string strValue, int strDay)
{
try
{
HttpCookie Cookie = new HttpCookie(strName);
//Cookie.Domain = ".zsjdc.com";//当要跨域名访问的时候,给cookie指定域名即可,格式为.xxx.com
Cookie.Expires = DateTime.Now.AddDays(strDay);
//Cookie.Value = Encryption(strValue);
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 static string getCookie(string strName)
{
HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName];
if (Cookie != null)
{
//return Decrypt(Cookie.Value.ToString());
return Cookie.Value.ToString();
}
else
{
return null;
}
}用我这个包没问题
localhost 没用
HttpCookie hc = new HttpCookie("test", Server.UrlEncode("welcome to bj!"));
hc.Expires = DateTime.Now.AddMinutes(15);
hc.Path = "/";
hc.Secure = false;
hc.HttpOnly = true;
Response.Cookies.Add(hc);
读取HttpCookie hc = Request.Cookies["test"];
if (hc == null)
{
Response.Write("没有");
}
else
{
Response.Write(Server.UrlDecode(hc.Value));
}