HttpWebRequest Cookie Handling

When I tried to automate the access to the web-based UI of the Dell DRAC 5 remote access controller, I stumbled upon a strange issue. System.Net.WebClient does not handle cookies on its own. You can force it to do so by intercepting the request and response handlers, GetWebRequest() and GetWebResponse(). There you have access to the HttpWebRequest and HttpWebResponse objects, and the respective CookieContainer and Cookies properties.

But given the nature of the DRAC 5, this is not enough. By default, HttpWebRequest interprets 302 (temporary redirect) responses and loads the next page. However, HttpWebRequest does not handle the cookies set on the 302 response. So you also have to disable AllowAutoRedirect to get the chance to save the cookies.

You can have a look at the code (using HttpWebRequest directly) in the github repository of DracWake.