C# HttpCookie 和 Cookie 的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1779410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:45:22  来源:igfitidea点击:

The difference between HttpCookie and Cookie?

c#asp.netcookieshttpcookiecookiejar

提问by Gio

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:

所以我很困惑,因为 msdn 和其他教程告诉我使用 HttpCookies 通过 Response.Cookies.Add(cookie) 添加 cookie。但这就是问题所在。Response.Cookies.Add 只接受 Cookies 而不是 HttpCookies,我得到这个错误:

cannot convert from 'System.Net.CookieContainer' to 'System.Net.Cookie'

无法从“System.Net.CookieContainer”转换为“System.Net.Cookie”

Additionally, what's the difference between Response.Cookies.Add(cookie) and Request.CookieContainer.Add(cookie)?

另外,Response.Cookies.Add(cookie) 和 Request.CookieContainer.Add(cookie) 有什么区别?

Thanks for the help in advance, I'm trying to teach myself using C#.

提前感谢您的帮助,我正在尝试使用 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);

采纳答案by Mehdi Golchin

You are using System.Net.HttpWebResponse. But the above example uses System.Web.HttpResponsewhich takes System.Web.HttpCookieas a parameter.

您正在使用System.Net.HttpWebResponse. 但是上面的例子使用System.Web.HttpResponsewhichSystem.Web.HttpCookie作为参数。

Scott Allen

斯科特艾伦

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

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

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

System.Net.HttpWebRequest 是用于向Web 应用程序发出传出请求的类。