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.

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top