2
votes

I am using HttpWebRequest with HttpWebResponse, the latter named response22 in my code, so here is a snippet from my code:

HttpWebResponse response22 = request22.GetResponse() as HttpWebResponse;
CookieCollection cookiezzz = new CookieCollection();
cookiezzz.Add(response22.Cookies);

foreach (System.Net.Cookie cookie in cookiezzz)
{
    MessageBox.Show(cookie.Name);
}

Strangely enough, Fiddler shows 5 cookies in the response, but when I iterate through the cookies, I get only four.

Also, my request is set to: equest22.AllowAutoRedirect = false;

Target framework is .Net 4.5, using WinForms

And using CookieContaner did not help at all as it "picks" only 2 of these cookies, but I don't want to worry about that right now, just want to figure out how to get all five cookies.

1
if you are on .net < 4, then .net treats www.sample.com and sample.com as two different domain, so do chk that! - Parimal Raj
Using .Net 4.5, updated my question. - TH Todorov

1 Answers

1
votes

Instead of trying to retrieve them from the response, you have to supply the cookie container to the request. That will force the container and the response cookies to be filled:

var cookiezzz = new CookieContainer();
request22.CookieContainer = cookiezzz;
HttpWebResponse response22 = request22.GetResponse() as HttpWebResponse;

foreach (System.Net.Cookie cookie in cookiezzz)
{
    MessageBox.Show(cookie.Name);
}