HttpCookie 和 Cookie 的区别?
所以我很困惑,因为 msdn 和其他教程告诉我使用 HttpCookies 通过 Response.Cookies.Add(cookie) 添加 cookie。但这就是问题所在。 Response.Cookies.Add 只接受 Cookies 而不是 HttpCookies,我收到此错误:
无法从 'System.Net.CookieContainer' 转换为 'System.Net.Cookie'
另外, Response.Cookies.Add(cookie) 和 之间有什么区别Request.CookieContainer.Add(cookie)?
提前感谢您的帮助,我正在尝试自学使用 C#。
// Cookie
Cookie MyCookie = new Cookie();
MyCookie.Name = "sid";
MyCookie.Value = SID;
MyCookie.HttpOnly = true;
MyCookie.Domain = ".domain.com";
// HttpCookie
HttpCookie MyCookie = new HttpCookie("sid");
MyCookie.Value = SID;
MyCookie.HttpOnly = true;
MyCookie.Domain = ".domain.com";
Response.Cookies.Add(MyCookie);
So I'm confused as msdn and other tutorials tell me to use HttpCookies to add cookies via Response.Cookies.Add(cookie). But that's the problem. Response.Cookies.Add only accepts Cookies and not HttpCookies and I get this error:
cannot convert from 'System.Net.CookieContainer' to 'System.Net.Cookie'
Additionally, what's the difference between Response.Cookies.Add(cookie) and Request.CookieContainer.Add(cookie)?
Thanks for the help in advance, I'm trying to teach myself using C#.
// Cookie
Cookie MyCookie = new Cookie();
MyCookie.Name = "sid";
MyCookie.Value = SID;
MyCookie.HttpOnly = true;
MyCookie.Domain = ".domain.com";
// HttpCookie
HttpCookie MyCookie = new HttpCookie("sid");
MyCookie.Value = SID;
MyCookie.HttpOnly = true;
MyCookie.Domain = ".domain.com";
Response.Cookies.Add(MyCookie);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用
System.Net.HttpWebResponse
。但上面的示例使用System.Web.HttpResponse
,它以System.Web.HttpCookie
作为参数。斯科特·艾伦
You are using
System.Net.HttpWebResponse
. But the above example usesSystem.Web.HttpResponse
which takesSystem.Web.HttpCookie
as a parameter.Scott Allen