发送使用Cookie HttpCookieCollection和的CookieContainer [英] Sending cookies using HttpCookieCollection and CookieContainer

查看:1228
本文介绍了发送使用Cookie HttpCookieCollection和的CookieContainer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过从我的服务器到远程服务器的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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆