Sending cookies using HttpCookieCollection and CookieContainer

前端 未结 3 1704
执笔经年
执笔经年 2020-12-15 05:00

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 t

3条回答
  •  佛祖请我去吃肉
    2020-12-15 05:37

    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 );
    }
    

提交回复
热议问题