问题描述
我想通过从我的服务器到远程服务器的HTTP请求的隧道,穿过所有的饼干。所以,我创建了一个新的的HttpWebRequest
对象,并希望在其上设置cookie。
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
的类型是 System.Net.CookieContainer
持有 System.Net .Cookies
。
在我传入的请求对象:
的Htt prequest.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?
推荐答案
这里的code我用从传入请求传送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.)
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 );
}
这篇关于发送使用Cookie HttpCookieCollection和的CookieContainer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!