djohns505
djohns505

Reputation: 216

Cannot get cookies from HttpWebResponse on Windows Phone 7

I am thoroughly confused as to what is the problem here. My understanding of the CookieContainer must be totally wrong. I am trying to log in to a website hosted on my machine. I have fiddler set up as a proxy, so I am able to read the traffic.

Every time I try to view the cookies on windows phone, the cookie container is always empty. I have tried so many different things to be able to get that container filled, but everything has failed. This is the last thing I tried:

        CookieContainer cc = new CookieContainer();
        HttpWebRequest request = WebRequest.CreateHttp("http://localhost:8888/account/logon");
        request.ContentType = "application/x-www-form-urlencoded";
        request.Method = "POST";
        request.CookieContainer = cc;
        request.BeginGetRequestStream(streamResult =>
        {
            HttpWebRequest xR = streamResult.AsyncState as HttpWebRequest;
            Stream stream = xR.EndGetRequestStream(streamResult);
            StreamWriter sw = new StreamWriter(stream);
            sw.Write("username=user1&password=1111");
            sw.Close();

            xR.BeginGetResponse(responseResult =>
            {
                HttpWebRequest yR = responseResult.AsyncState as HttpWebRequest;
                HttpWebResponse response = yR.EndGetResponse(responseResult) as HttpWebResponse;
                response.Close();

                Dispatcher.BeginInvoke(() => MessageBox.Show("cc count: " + cc.Count.ToString() + "\nresponse count: " + response.Cookies.Count));
            }, xR);

        }, request);

However, if I use the exact same code in a console app, the container "cc" has the cookies!

        CookieContainer cc = new CookieContainer();
        HttpWebRequest request = WebRequest.CreateHttp("http://localhost:8888/account/logon");
        request.ContentType = "application/x-www-form-urlencoded";
        request.Method = "POST";
        request.CookieContainer = cc;
        request.BeginGetRequestStream(streamResul =>
        {
            HttpWebRequest xR = streamResul.AsyncState as HttpWebRequest;
            Stream stream = xR.EndGetRequestStream(streamResul);
            StreamWriter sw = new StreamWriter(stream);
            sw.Write("username=user1&password=1111");
            sw.Close();

            xR.BeginGetResponse(responseResult =>
            {
                HttpWebRequest yR = responseResult.AsyncState as HttpWebRequest;
                HttpWebResponse response = yR.EndGetResponse(responseResult) as HttpWebResponse;
                response.Close();

                Console.WriteLine("cc count: {0}\nresponse count: {1}", cc.Count.ToString(), response.Cookies.Count);
            }, xR);

        }, request);

        Console.Read();

The server is sending back both cookies that I am looking for each time I test it. However for whatever reason, the windows phone code does not get those cookies. This is pretty much the same as this question: Cannot get cookies in wp7 using HttpWebRequest however I don't understand what the solution was.

Upvotes: 0

Views: 803

Answers (1)

djohns505
djohns505

Reputation: 216

Wow, I feel like a fool. It has been working all along... Maybe to others this is obvious, but I missed it. The problem is that since the cookies that are being sent back are HttpOnly, the cookie container does not expose them at all, even if you ask for the count of cookies.

It is strange though that in the console version, I can see that there are two, but I suppose the System.Net.dll for windows phone locked down the CookieContainer a little more.

Upvotes: 1

Related Questions