在winfrom中如何操作cookie

lshfong 2009-12-24 11:48:43
HttpCookie cookie = new HttpCookie("ReportCookie");
cookie.Values["a"] = "中华人民共和国";
cookie.Values["b"] = "大家好";
DateTime dtNow = DateTime.Now;
TimeSpan tsMinute = new TimeSpan(0, 1, 0, 0);
cookie.Expires = dtNow + tsMinute;
HttpContext.Current.Response.AppendCookie(cookie);
我在程序中这样写的,运行后出现错误,提示“未将对象引用设置到对象的实例。”
=========================================================
请高人指点一下啊!
...全文
112 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lshfong 2009-12-24
  • 打赏
  • 举报
回复
我是一个c/s结构的程序,要和服务器进行交互
wefgod 2009-12-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yangheation 的回复:]
Winform里有必要用过COOKIE吗?直接用实体类就能解决问题啦
[/Quote]他是不是想自己写浏览器?
wefgod 2009-12-24
  • 打赏
  • 举报
回复
    protected void Page_Load(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
// Get cookie from the current request.
HttpCookie cookie = Request.Cookies.Get("DateCookieExample");

// Check if cookie exists in the current request.
if (cookie == null)
{
sb.Append("Cookie was not received from the client. ");
sb.Append("Creating cookie to add to the response. <br/>");
// Create cookie.
cookie = new HttpCookie("DateCookieExample");
// Set value of cookie to current date time.
cookie.Value = DateTime.Now.ToString();
// Set cookie to expire in 10 minutes.
cookie.Expires = DateTime.Now.AddMinutes(10d);
// Insert the cookie in the current HttpResponse.
Response.Cookies.Add(cookie);
}
else
{
sb.Append("Cookie retrieved from client. <br/>");
sb.Append("Cookie Name: " + cookie.Name + "<br/>");
sb.Append("Cookie Value: " + cookie.Value + "<br/>");
sb.Append("Cookie Expiration Date: " +
cookie.Expires.ToString() + "<br/>");
}
Label1.Text = sb.ToString();
}


http://msdn.microsoft.com/zh-cn/library/system.web.httpcookie(VS.80).aspx
可以参考下
没有访问权限 2009-12-24
  • 打赏
  • 举报
回复
Winform里有必要用过COOKIE吗?直接用实体类就能解决问题啦

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧