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.