Handling Cookies with Redirects and HttpWebRequest

The HttpWebRequest handles redirects automatically. That's usually a nice feature but it can actually get in the way when the web server is setting cookies in the same response in which it is sending a redirect.

The HttpWebRequest handles redirects automatically. That's usually a nice feature but it can actually get in the way when the web server is setting cookies in the same response in which it is sending a redirect. For some odd reason the HttpWebRequest object will totally discard cookies that are set with a redirect response. I ran into a problem with this when I was working in some code that interfaced to Google Voice and it was driving me crazy since I didn't know where the problem was coming from.

Once I figured out what was happening, the solution to the problem was simple. The first step is to disable the class's automatic response to redirect request. When a response is returned from the class, it's necessary to check the response to see if it includes a redirect and if so create a new request. Since a redirect could occur more than once, it is necessary to do this in a loop. With each new WebRequest that is created, it is necessary to set the CookiesContainer member.

Trying to perform this same thing on the .NET Compact Framework is a little more challenging since it doesn't support cookies at all. I'll discuss the solution I used in another post within the next few days.

Share

About the Author

I attended Southern Polytechnic State University and earned a Bachelors of Science in Computer Science and later returned to earn a Masters of Science in Software Engineering.

For the past few years I've been providing solutions to clients using Microsoft technologies for web and Windows applications.

While most of my CodeProject.com articles are centered around Windows Phone it is only one of the areas in which I work and one of my interests. I also have interest in mobile development on Android and iPhone. Professionally I work with several Microsoft technologies including SQL Server technologies, Silverlight/WPF, ASP.Net and others. My recreational development interest are centered around Artificial Inteligence especially in the area of machine vision.

As you indicated, the request will automatically redirect by default, but won't automatically include the cookies in the redirect. What I found was that it was easily resolved by just adding a cookie container to the request. I know the person below my post said the same thing, maybe not as clearly. Try the above code, it does work.

Hi therem
In your article you mention that you had a solution for this in CF and that you would post it at some point. As far as I can tell you never did post that article. Would you be willing to send me your solution? I am currently having issues with this exact problem and any help would greatly be appreciated.

Thank you very much for your article. I experienced the same problem and worked on it for several hours until I found your solution. But I had to extend it al little bit. After response.Close() I hat to add a loop which manually adds the responded cookies to my CookieContainer. Like that:

response.Close()
ForEach c As System.Net.Cookie In response.Cookies
Cookies.Add(c)
Next