使用 HttpCookieCollection 和 CookieContainer 发送 cookie
我想通过 HTTP 请求从我的服务器传输到远程服务器,并传递所有 cookie。 因此,我创建了一个新的 HttpWebRequest 对象并希望在其上设置 cookie。
HttpWebRequest.CookieContainer
是包含 System.Net.Cookies
的 System.Net.CookieContainer
类型。
在我传入的请求对象上:
HttpRequest.Cookies
是 System.Web.HttpCookieCollection
类型,其中包含 System.Web.HttpCookies
。
基本上我希望能够将它们分配给彼此,但不同的类型使得这是不可能的。 我是否必须通过复制它们的值来转换它们,还是有更好的方法?
I want to tunnel through an HTTP request from my server to a remote server, passing through all the cookies. So I create a new HttpWebRequest
object and want to set cookies on it.
HttpWebRequest.CookieContainer
is type System.Net.CookieContainer
which holds System.Net.Cookies
.
On my incoming request object:
HttpRequest.Cookies
is type System.Web.HttpCookieCollection
which holds System.Web.HttpCookies
.
Basically I want to be able to assign them to each other, but the differing types makes it impossible. Do I have to convert them by copying their values, or is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我用来将 cookie 对象从传入请求传输到新的 HttpWebRequest 的代码...(“myRequest”是我的 HttpWebRequest 对象的名称。)
Here's the code I've used to transfer the cookie objects from the incoming request to the new HttpWebRequest... ("myRequest" is the name of my HttpWebRequest object.)
我今天需要为使用基于表单的身份验证 (FBA) 的 SharePoint 网站执行此操作。 如果您尝试调用应用程序页面而不克隆 cookie 并分配 CookieContainer 对象,则请求将失败。
我选择将工作抽象为这个方便的扩展方法:
然后,您可以从任何 HttpRequest 对象调用它,并将目标 HttpWebRequest 对象作为参数,例如:
其中 TargetUrl 是我所在页面的 Url,SourceRequest 是 HttpRequest我当前所在页面的信息,通过 Page.Request 检索。
I had a need to do this today for a SharePoint site which uses Forms Based Authentication (FBA). If you try and call an application page without cloning the cookies and assigning a CookieContainer object then the request will fail.
I chose to abstract the job to this handy extension method:
You can then just call it from any HttpRequest object with a target HttpWebRequest object as a parameter, for example:
where TargetUrl is the Url of the page I am after and SourceRequest is the HttpRequest of the page I am on currently, retrieved via Page.Request.
大卫的建议是正确的。 你需要复制。 只需简单地创建函数来重复复制即可。 创建 HttpCookie 和 Cookie 对象是为了确保我们可以区分其功能和来源。 用户和代理之间使用的 HttpCookie 代理和远程 Web 服务器之间使用 Cookie。
HttpCookie 的功能较少,因为 cookie 是由您发起的,并且您知道如何处理它。 Cookie 为您提供管理从网络服务器接收的 cookie。 与 CookieContainer 一样,它可用于管理域、路径和过期时间。
所以用户端和网络服务器端是不同的,要连接它,你肯定需要转换它。 就您而言,它只是简单的直接赋值。
请注意,CookieContainer 在 .Add(Cookie) 和 .GetCookies(uri) 方法上存在错误。
请在此处查看详细信息并修复:
http ://dot-net-expertise.blogspot.com/2009/10/cookiecontainer-domain-handling-bug-fix.html
CallMeLaNN
The suggested from David is the right one. You need to copy. Just simply create function to copy repeatedly. HttpCookie and Cookie object is created to make sure we can differentiate both in its functionality and where it come. HttpCookie used between user and your proxy Cookie is used between your proxy and remote web server.
HttpCookie has less functionality since the cookie is originated from you and you know how to handle it. Cookie provide you to manage cookie received from web server. Like CookieContainer, it can be used to manage domain, path and expiry.
So user side and web server side is different and to connect it, sure you need to convert it. In your case, it just simply direct assignment.
Notice that CookieContainer has a bug on .Add(Cookie) and .GetCookies(uri) method.
See the details and fix here:
http://dot-net-expertise.blogspot.com/2009/10/cookiecontainer-domain-handling-bug-fix.html
CallMeLaNN