HttpWebRequest and Set-Cookie header in response not parsed (WP7)
Solution 1
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);
Solution 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.
Related videos on Youtube
Brent Dunham
Updated on June 04, 2022Comments
-
Brent Dunham almost 2 years
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 usingbool 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?
-
Brent Dunham over 13 yearsIf you meant setting the header to something before the request, I tried that to no avail.
-
Hoàng Vũ Tgtt over 8 yearsCan You help me? What function GetData() is? (in your last line)
-
0x2D over 8 years
GetData
is a dummy function in this case - you can just create your own callback. -
IvanP over 3 yearsNope. Not true.