简体   繁体   中英

HttpWebResponse does not return all cookies

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.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM