Click here to Skip to main content
Licence CPOL
First Posted 28 Dec 2009
Views 9,352
Bookmarked 2 times

Handling Cookies with Redirects and HttpWebRequest

By Joel Ivory Johnson | 28 Dec 2009 | Technical Blog
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.

1

2
1 vote, 50.0%
3

4
1 vote, 50.0%
5
4.33/5 - 2 votes
μ 4.33, σa 2.47 [?]
A Technical Blog article. View original blog here.[^]

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.

A simplified version of my code looks like the following:

HttpWebRequest GetNewRequest(string targetUrl)
{
     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUrl);
     request.CookieContainer = SessionCookieContainer;
     request.AllowAutoRedirect = false;
     return request;
}

HttpWebRequest request = GetNewRequest(targetUrl);
HttpWebResponse response= (HttpWebResponse)request.GetResponse();

while (response.StatusCode == HttpStatusCode.Found)
{
     response.Close();
     request = GetNewRequest(response.Headers["Location"]);
     response= (HttpWebResponse)request.GetResponse();
}

//--At this point the redirected response is ready to be used

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Joel Ivory Johnson

Software Developer

United States United States

Member

Follow on Twitter Follow on Twitter
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.
 


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionThx man! It was drving me crazy... Pinmemberyuval12348:28 28 Jun '11  
GeneralNice finding! PinmemberSohel_Rana4:57 28 Dec '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120209.1 | Last Updated 28 Dec 2009
Article Copyright 2009 by Joel Ivory Johnson
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid