HttpWebRequest and Set-Cookie header in response not parsed (WP7)

Brent Dunham picture Brent Dunham · Nov 22, 2010 · Viewed 17.8k times · Source

I am trying to get the header "Set-Cookie" or access the cookie container, but the Set-Cookie header is not available. The cookie is in the response header, but it's not there in the client request object. I am registering the ClientHttp stack using

bool httpResult = WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);

Here's the response:

HTTP/1.1 200 OK
Content-Type: application/xml; charset=utf-8
Connection: keep-alive
Status: 200
X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 3.0.0.pre4
ETag: "39030a9c5a45a24e485e4d2fb06c6389"
Client-Version: 312, 105, 0, 0
X-Runtime: 44
Content-Length: 1232
Set-Cookie: _CWFServer_session=[This is the session data]; path=/; HttpOnly
Cache-Control: private, max-age=0, must-revalidate
Server: nginx/0.7.67 + Phusion Passenger 3.0.0.pre4 (mod_rails/mod_rack)

<?xml version="1.0" encoding="UTF-8"?>
<user>
...
</user>

My callback code contains something like:

var webRequest = (HttpWebRequest)result.AsyncState;
raw = webRequest.EndGetResponse(result) as HttpWebResponse;
foreach (Cookie c in webRequest.CookieContainer.GetCookies(webRequest.RequestUri))
{
    Console.WriteLine("Cookie['" + c.Name + "']: " + c.Value);
}

I've also tried looking at the headers but Set-Cookie header isn't present in the response either.

Any suggestions on what may be the problem?

Answer

Den Delimarsky picture Den Delimarsky · Nov 22, 2010

Try explicitly passing a new CookieContainer:

CookieContainer container = new CookieContainer();
container.Add(new Uri("http://yoursite"), new Cookie("name", "value"));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://yoursite");
request.CookieContainer = container;
request.BeginGetResponse(new AsyncCallback(GetData), request);