HttpCookie 和 Cookie 的区别?

发布于 2024-08-12 06:16:08 字数 682 浏览 1 评论 0原文

所以我很困惑,因为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韵柒 2024-08-19 06:16:08

您正在使用System.Net.HttpWebResponse。但上面的示例使用 System.Web.HttpResponse ,它以 System.Web.HttpCookie 作为参数。

斯科特·艾伦

System.Web.HttpRequest是一个使用的类
在服务器上和 ASP.NET 内部
应用。它代表的是
来自客户端的传入请求。

System.Net.HttpWebRequest 是一个类
用于发出传出请求
一个网络应用程序。

You are using System.Net.HttpWebResponse. But the above example uses System.Web.HttpResponse which takes System.Web.HttpCookie as a parameter.

Scott Allen

System.Web.HttpRequest is a class used
on the server and inside an ASP.NET
application. It represents the
incoming request from a client.

System.Net.HttpWebRequest is a class
used to make an outgoing request to
a web application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文