Search code examples
c#cookieshttpwebrequestcookiecontainer

Passing cookies to httpwebrequest


Is it possible to pass cookies to HttpWebRequest without using property HttpWebRequest.CookieContainer?

For some reasons, I don't have access to some object properties including HttpWebRequest.CookieContainer. I do have access to HttpWebRequest class and CookieContainer class.


Solution

  • I'm not sure why you would want to ignore the existence of an existing mechanism and reinvent the wheel, possibly including bugs solved by the existing implementation.

    Setting cookies is trivial though:

    request.Headers["Cookie"] = "foo=bar";
    

    Just as reading them from the response:

    string cookieHeader = response.Headers["Set-Cookie"];