C# HttpWebRequest: Cookie is downloaded but collections are empty!

Posted on 2012-09-13
Last Modified: 2012-11-04
The following does a simple GET from a classic ASP page:
CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.somesite.asp");            
request.UserAgent = "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)";
request.CookieContainer = cookieContainer;
request.KeepAlive = true;
request.Accept = null;
request.UseDefaultCredentials = true;
request.Proxy = null;
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Console.WriteLine("StatusCode=" + response.StatusCode);

Console.WriteLine("cookieContainer.Count=" + cookieContainer.Count);

Console.WriteLine("response.Cookies.Count=" + response.Cookies.Count)

Open in new window

But both collections are empty.  Any ideas why?

Here is a dump of the WebHeaderCollection:
Content-Length: 128045
Content-Type: text/html
Date: Thu, 13 Sep 2012 23:46:59 GMT
Expires: Wed, 12 Sep 2012 23:46:58 GMT
Server: Microsoft-IIS/6.0
Cache-control: private
The cookie is clearly there!
Question by:ZuZuPetals
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    Have you tried inspecting the cookies of the response?
    LVL 2

    Author Comment

    Yes, please see line 16.
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    Ah, I completely glossed over that bit. I'll see if I can test it out later this evening.
    LVL 2

    Accepted Solution

    I have no idea why the cookie isn't in one of the two collection.  However, the solution is to just parse the raw HTTP headers, grab the cookie information, and instantiate a cookie object.
    LVL 2

    Author Closing Comment

    No answer was given by community, but I offered a workaround.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now