Click here to Skip to main content
14,934,216 members
Articles / Multimedia / GDI
Technical Blog
Posted 28 Dec 2009

Tagged as

Stats

81.2K views
13 bookmarked

Handling Cookies with Redirects and HttpWebRequest

Rate me:
Please Sign up or sign in to vote.
4.50/5 (4 votes)
28 Dec 2009CPOL1 min read
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.

A simplified version of my code looks like the following:

C++
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)

Share

About the Author

Joel Ivory Johnson
Software Developer
United States United States
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. I've largely developed solutions that are based on a mix of Microsoft technologies with open source technologies mixed in. I've got an interest in astronomy and you'll see that interest overflow into some of my code project articles from time to time.



Twitter:@j2inet

Instagram: j2inet


Comments and Discussions

 
QuestionSimplest way Pin
Jols10016-May-14 12:30
MemberJols10016-May-14 12:30 
QuestionManaged to handle this with less code Pin
Ido Flatow12-May-13 4:01
MemberIdo Flatow12-May-13 4:01 
AnswerRe: Managed to handle this with less code Pin
johan wettstrom11-Jul-13 12:11
Memberjohan wettstrom11-Jul-13 12:11 
QuestionCompact Framework Pin
amura.cxg23-Apr-13 15:53
Memberamura.cxg23-Apr-13 15:53 
SuggestionHad to manually add the response cookie Pin
xbertx16-Apr-12 23:46
Memberxbertx16-Apr-12 23:46 
QuestionThx man! It was drving me crazy... Pin
yuval123428-Jun-11 7:28
Memberyuval123428-Jun-11 7:28 
GeneralNice finding! Pin
Sohel_Rana28-Dec-09 3:57
MemberSohel_Rana28-Dec-09 3:57 

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

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