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

18,392

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.

Share:
18,392

Related videos on Youtube

Brent Dunham
Author by

Brent Dunham

Updated on June 04, 2022

Comments

  • Brent Dunham
    Brent Dunham about 14 hours

    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?

  • Brent Dunham
    Brent Dunham over 11 years
    If you meant setting the header to something before the request, I tried that to no avail.
  • Hoàng Vũ Tgtt
    Hoàng Vũ Tgtt over 6 years
    Can You help me? What function GetData() is? (in your last line)
  • Den Delimarsky
    Den Delimarsky over 6 years
    GetData is a dummy function in this case - you can just create your own callback.
  • IvanP
    IvanP over 1 year
    Nope. Not true.