学员:徐豫锋提问时间:2008-05-27 09:22:42
我试了一下书上的cookie 就是不明白,问题出在哪里
有两个按钮“创建”,“接收”
protected void Button1_Click(object sender, EventArgs e)
{
singlevaluecookie = new HttpCookie("test1");
singlevaluecookie.Value="hhhhh";
singlevaluecookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(singlevaluecookie);
}
protected void Button2_Click(object sender, EventArgs e)
{
HttpCookie singlevaluecookie1 = Response.Cookie["test1"];
if (singlevaluecookie1 != null)
{
Response.Write("接受到的值"+singlevaluecookie1.Value);
}
}
按了“接收”,只显示“接受到的值”,真不知道哪里出了问题?
回答人:polarstar (.NET学习顾问)回答时间:2008-05-27 17:32:08
接受的时候应该这样:
protected void Button2_Click(object sender, EventArgs e)
{
HttpCookie singlevaluecookie1 = Request.Cookies["test1"];//注意等号右边是Request而不是Response
if (singlevaluecookie1 != null)
{
Response.Write("接受到的值"+singlevaluecookie1.Value);
}
}
看看微软对Response的定义:
Provides access to the output stream for the current page. You can use this class to inject text into the page, to write cookies, and more.
对当前页面输出流提供访问,你可以使用这个类往页面写入文本、写cookies等等。
Request定义:
Provides access to the current page request, including the request headers, cookies, client certificate, query string, and so on. You can use this class to read what the browser has sent.
对当前页面请求提供访问,包括请求标头(Request headers)、cookies、客户证书、查询字串等等,你可以使用这个类类读浏览器发送过来的东西。
学员对该答案的评价 非常感谢你,是我买的书有错,郁闷死了(一晚上睡不着,甚至自己是不是学计算机的料) 再麻烦一下: 看看微软对Response的定义: Provides access to the output stream for the current page. You can use this class to inject text into the page, to write cookies, and more. 对当前页面输出流提供访问,你可以使用这个类往页面写入文本、写cookies等等。 Reques