C# 使用 HttpCookieCollection 和 CookieContainer 发送 cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1214387/
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
Sending cookies using HttpCookieCollection and CookieContainer
提问by Mike
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.
我想通过从我的服务器到远程服务器的 HTTP 请求隧道,传递所有 cookie。所以我创建了一个新HttpWebRequest
对象并想在它上面设置 cookie。
HttpWebRequest.CookieContainer
is type System.Net.CookieContainer
which holds System.Net.Cookies
.
HttpWebRequest.CookieContainer
是System.Net.CookieContainer
包含System.Net.Cookies
.
On my incoming request object:
在我传入的请求对象上:
HttpRequest.Cookies
is type System.Web.HttpCookieCollection
which holds System.Web.HttpCookies
.
HttpRequest.Cookies
是System.Web.HttpCookieCollection
包含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?
基本上我希望能够将它们相互分配,但是不同的类型使它不可能。我是否必须通过复制它们的值来转换它们,还是有更好的方法?
采纳答案by David
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.)
这是我用来将 cookie 对象从传入请求传输到新的 HttpWebRequest 的代码......(“myRequest”是我的 HttpWebRequest 对象的名称。)
HttpCookieCollection oCookies = Request.Cookies;
for ( int j = 0; j < oCookies.Count; j++ )
{
HttpCookie oCookie = oCookies.Get( j );
Cookie oC = new Cookie();
// Convert between the System.Net.Cookie to a System.Web.HttpCookie...
oC.Domain = myRequest.RequestUri.Host;
oC.Expires = oCookie.Expires;
oC.Name = oCookie.Name;
oC.Path = oCookie.Path;
oC.Secure = oCookie.Secure;
oC.Value = oCookie.Value;
myRequest.CookieContainer.Add( oC );
}
回答by 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 和 Cookie 对象是为了确保我们可以区分其功能和来源。用户和您的代理之间使用的 HttpCookie Cookie 用于您的代理和远程 Web 服务器之间。
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.
HttpCookie 的功能较少,因为 cookie 来自您并且您知道如何处理它。Cookie 为您提供管理从网络服务器接收的 cookie。和 CookieContainer 一样,它可以用来管理域、路径和过期时间。
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.
所以用户端和Web服务器端是不同的,要连接它,肯定需要转换它。在您的情况下,它只是简单的直接分配。
Notice that CookieContainer has a bug on .Add(Cookie) and .GetCookies(uri) method.
请注意 CookieContainer 在 .Add(Cookie) 和 .GetCookies(uri) 方法上有一个错误。
See the details and fix here:
在此处查看详细信息并修复:
http://dot-net-expertise.blogspot.com/2009/10/cookiecontainer-domain-handling-bug-fix.html
http://dot-net-expertise.blogspot.com/2009/10/cookiecontainer-domain-handling-bug-fix.html
CallMeLaNN
呼叫MeLaNN
回答by Colin Gardner
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.
我今天需要为使用基于表单的身份验证 (FBA) 的 SharePoint 网站执行此操作。如果您尝试在不克隆 cookie 和分配 CookieContainer 对象的情况下调用应用程序页面,则请求将失败。
I chose to abstract the job to this handy extension method:
我选择将工作抽象为这个方便的扩展方法:
public static CookieContainer GetCookieContainer(this System.Web.HttpRequest SourceHttpRequest, System.Net.HttpWebRequest TargetHttpWebRequest)
{
System.Web.HttpCookieCollection sourceCookies = SourceHttpRequest.Cookies;
if (sourceCookies.Count == 0)
return null;
else
{
CookieContainer cookieContainer = new CookieContainer();
for (int i = 0; i < sourceCookies.Count; i++)
{
System.Web.HttpCookie cSource = sourceCookies[i];
Cookie cookieTarget = new Cookie() { Domain = TargetHttpWebRequest.RequestUri.Host,
Name = cSource.Name,
Path = cSource.Path,
Secure = cSource.Secure,
Value = cSource.Value };
cookieContainer.Add(cookieTarget);
}
return cookieContainer;
}
}
You can then just call it from any HttpRequest object with a target HttpWebRequest object as a parameter, for example:
然后,您可以使用目标 HttpWebRequest 对象作为参数从任何 HttpRequest 对象调用它,例如:
HttpWebRequest request;
request = (HttpWebRequest)WebRequest.Create(TargetUrl);
request.Method = "GET";
request.Credentials = CredentialCache.DefaultCredentials;
request.CookieContainer = SourceRequest.GetCookieContainer(request);
request.BeginGetResponse(null, null);
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.
其中 TargetUrl 是我访问的页面的 Url,SourceRequest 是我当前所在页面的 HttpRequest,通过 Page.Request 检索。