2

I am experiencing some strange behavior with a Windows Phone 8 app that I am building and I hope someone here has some experience with it.

I am reading a website using a normal HttpWebRequest and expecting a cookie as a response. However, somehow, I am not getting the Set-cookie header back in my WebResponse. I have created the same functionality under WPF and it works as normal - returns the Set-cookie header in the response.

I have also tried looking at the CookieContainer of the response, but it is also empty.

Here is the code that I am using for this. Note: the same piece of code (without the async stuff) works correct in WPF and returns the Set-Cookie header. I can post it as well if necessary:

  HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.mysite.com/login");

  request.Method = HttpMethod.Post;
  request.AllowAutoRedirect = false;//normally there is a redirect in place

  postData = "username=1234&password=2345";
  var data = Encoding.UTF8.GetBytes(postData);

  request.ContentType = "application/x-www-form-urlencoded";
  request.ContentLength = data.Length;

using (var stream = await Task.Factory.FromAsync<Stream>(request.BeginGetRequestStream, request.EndGetRequestStream, null))
    {
        await stream.WriteAsync(data, 0, data.Length);
        stream.Close();
    }

                using (var response = await Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null))
    {
         return response.Headers["set-cookie"];
    }

As a result of this, I am getting some response headers (such as content-type and server specific ones) but not the Set-Cookie one.

sTodorov
  • 5,435
  • 5
  • 35
  • 55
  • 1
    I've seen really fishy behavior with cookies on Windows Phone, so I'm not surprised. I think the first step is to use Fiddler to capture the response and make sure the `set-cookie` header is returned by the server. Then, try running your code again by enabling Visual Studio to break on all exceptions (first, disable `Just my code` http://msdn.microsoft.com/en-us/library/dn457346.aspx then press control + alt + E and check the box in front of "Common Language Runtime Exceptions" – Kevin Gosse Mar 20 '14 at 07:06
  • I also encountered the same problem used jquery instead. – Praveen Mar 20 '14 at 08:25
  • Thanks for the suggestions to @KooKiz and @Praveen! I will give them a try and report back – sTodorov Mar 20 '14 at 16:00

1 Answers1

2

I've done some more tests and the set-cookie header is omitted only on the Windows Phone Emulator. When debugging with an actual device, the header is received as expected.

It is still pretty strange to me why the emulator behaves this way. I saw many posts on issues with http-only cookies in the emulator but none with a concrete reason.

UPDATE:

Testing on the 8.0.10322 emulator works just fine - cookies are handled correctly.It looks as the default phone emulator does something fishy with the cookies.

sTodorov
  • 5,435
  • 5
  • 35
  • 55