我在App_Code中写了有关写入Cookies和读取cookies的类(pub.cs) /// <summary>
/// 写入带子键的cookies
/// </summary>
/// <param name="strkey"></param>
///<param name="strsubkey"></param>
///<param name="strvalue"></param>
/// <returns></returns>
public void WriteSubCookies(string strkey,string strsubkey,string strvalue)
{
if ((strkey.Length)>=0&&(strsubkey.Length)>=0)
{
System.Web.HttpCookie newcookie=new HttpCookie(strkey);
newcookie.Values[strsubkey]=strvalue;
newcookie.Expires=DateTime.Now.AddDays(1);
System.Web.HttpContext.Current.Response.AppendCookie(newcookie);
}
}
/// <summary>
/// 读取带子键的cookies
/// </summary>
/// <param name="strkey"></param>
///<param name="strsubkey"></param>
/// <returns></returns>
public void GetSubCookies(string strkey,string strsubkey)
{
if ((System.Web.HttpContext.Current.Request.Cookies[strkey]) != null)
{
string getsubcookiesresult;
System.Web.HttpContext.Current.Response.Write(System.Web.HttpContext.Current.Server.HtmlEncode(System.Web.HttpContext.Current.Request.Cookies[strkey][strsubkey])); }
else
{
System.Web.HttpContext.Current.Response.Write("");
}
}但是读取的时候,只能读取最后写入的cookies
===================================================
测试:public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//protected HtmlGenericControl title;
// this.title.innerText=pub.city;
pub mypub = new pub();
mypub.WriteSubCookies("mypub","username","张晓初");
mypub.WriteSubCookies("mypub", "userid", "20080001");
mypub.GetSubCookies("mypub","username");
mypub.GetSubCookies("mypub","userid");
}
}
============
结果:
20080001
("张晓初"并没有读出来,不知道是什么原因?)
/// 写入带子键的cookies
/// </summary>
/// <param name="strkey"></param>
///<param name="strsubkey"></param>
///<param name="strvalue"></param>
/// <returns></returns>
public void WriteSubCookies(string strkey,string strsubkey,string strvalue)
{
if ((strkey.Length)>=0&&(strsubkey.Length)>=0)
{
System.Web.HttpCookie newcookie=new HttpCookie(strkey);
newcookie.Values[strsubkey]=strvalue;
newcookie.Expires=DateTime.Now.AddDays(1);
System.Web.HttpContext.Current.Response.AppendCookie(newcookie);
}
}
/// <summary>
/// 读取带子键的cookies
/// </summary>
/// <param name="strkey"></param>
///<param name="strsubkey"></param>
/// <returns></returns>
public void GetSubCookies(string strkey,string strsubkey)
{
if ((System.Web.HttpContext.Current.Request.Cookies[strkey]) != null)
{
string getsubcookiesresult;
System.Web.HttpContext.Current.Response.Write(System.Web.HttpContext.Current.Server.HtmlEncode(System.Web.HttpContext.Current.Request.Cookies[strkey][strsubkey])); }
else
{
System.Web.HttpContext.Current.Response.Write("");
}
}但是读取的时候,只能读取最后写入的cookies
===================================================
测试:public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//protected HtmlGenericControl title;
// this.title.innerText=pub.city;
pub mypub = new pub();
mypub.WriteSubCookies("mypub","username","张晓初");
mypub.WriteSubCookies("mypub", "userid", "20080001");
mypub.GetSubCookies("mypub","username");
mypub.GetSubCookies("mypub","userid");
}
}
============
结果:
20080001
("张晓初"并没有读出来,不知道是什么原因?)
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货