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

4

5

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?

Collimate answered 22/11, 2010 at 18:8 Comment(1)
The "Set-Cookie" header seems to be missing from my HttpWebResponses on WP7 too.Puett
M
6

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);
Mabel answered 22/11, 2010 at 22:45 Comment(2)
Can You help me? What function GetData() is? (in your last line)Tulle
GetData is a dummy function in this case - you can just create your own callback.Mabel
O
2

You are receiving HttpOnly cookies:

Set-Cookie: _CWFServer_session=[This is the session data]; path=/; HttpOnly 

For security reasons, those cookies can't be accessed from code, but you still can use them in your next calls to HttpWebRequest. More on this here : Reading HttpOnly Cookies from Headers of HttpWebResponse in Windows Phone

With WP7.1, I also had problems reading non HttpOnly cookies. I found out that they are not available if the response of the HttpWebRequest comes from the cache. Making the query unique with a random number solved the cache problem :

// The Request
Random random = new Random();  
// UniqueQuery is used to defeat the cache system that destroys the cookie.
_uniqueQuery = "http://my-site.somewhere?someparameters=XXX"
       + ";test="+ random.Next();

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_uniqueQuery);
request.BeginGetResponse(Response_Completed, request);

Once you get the response, you can fetch the cookie from the response headers:

void Response_Completed(IAsyncResult result)
{
    HttpWebRequest request = (HttpWebRequest)result.AsyncState;
    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
    String header = response.Headers["Set-Cookie"]; 

I never managed to get the CookieContainer.GetCookies() method to work.

Olwena answered 13/4, 2012 at 5:32 Comment(0)
P
0

Is the cookie httponly? If so, you won't be able to see it, but if you use the same CookieContainer for your second request, the request will contain the cookie, even though your program won't be able to see it.

Puett answered 25/5, 2011 at 4:50 Comment(1)
Nope. Not true.Unexampled
Y
-1

You must edit the headers collection directly. Something like this:

request.Headers["Set-Cookie"] = "name=value";

request.BeginGetResponse(myCallback, request);
Yt answered 22/11, 2010 at 18:39 Comment(1)
If you meant setting the header to something before the request, I tried that to no avail.Collimate

© 2022 - 2024 — McMap. All rights reserved.