在Windows Phone应用程序上不发送cookie,但在Windows 8应用程序中以相同的代码发送cookie
本文关键字:应用程序 cookie Windows 代码 但在 程序上 Phone 应用 | 更新日期: 2023-09-27 17:50:22
我有一个使用HttpWebRequest
/HttpWebResponse
进行GET和POST请求的基本类。
我使用我的类登录到API,然后请求数据。在Windows 8"Metro"应用程序中,它完全按照预期工作。在Windows Phone 8应用程序中,登录似乎成功,但在随后的数据请求中,没有发送cookie,服务器响应,好像客户端未登录。
这是类,这完全相同的代码在Windows 8应用程序和Windows Phone应用程序中使用:
class Class1
{
CookieContainer cookieJar = new CookieContainer();
CookieCollection responseCookies = new CookieCollection();
public async Task<string> httpRequest(HttpWebRequest request)
{
string received;
using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))
{
using (var responseStream = response.GetResponseStream())
{
using (var sr = new StreamReader(responseStream))
{
cookieJar = request.CookieContainer;
responseCookies = response.Cookies;
received = await sr.ReadToEndAsync();
}
}
}
return received;
}
public async Task<string> get(string path)
{
var request = WebRequest.Create(new Uri(path)) as HttpWebRequest;
request.CookieContainer = cookieJar;
return await httpRequest(request);
}
public async Task<string> post(string path, string postdata)
{
var request = WebRequest.Create(new Uri(path)) as HttpWebRequest;
request.Method = "POST";
request.CookieContainer = cookieJar;
byte[] data = Encoding.UTF8.GetBytes(postdata);
using (var requestStream = await Task<Stream>.Factory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, null))
{
await requestStream.WriteAsync(data, 0, data.Length);
}
return await httpRequest(request);
}
}
和初始化请求的代码:
var n = new Class1();
await n.post("https://mydomain.com/api/login/", "username=myusername&password=mypassword");
await n.get("https://mydomain.com/reader/feeds/");
奇怪的是,如果我在域名前加上"www.",它在Windows Phone 8应用程序和Windows 8 Metro应用程序中都有效。
我认为这与如何处理域有关。cookie的域名是"。mydomain.com",如果没有前缀,它一定会认为cookie不属于该域名。经过一番搜索,我发现了一个报告,有人注意到类似的问题。
我不明白的是为什么这在Windows 8应用程序和Windows Phone应用程序中的处理方式不同,所以一行一行相同的代码在一个平台上工作,但在另一个平台上失败。
我对此做了更多的调查。
我使用的服务器代码在PHP中:<?php
if ($_REQUEST["what"] == "set")
{
setcookie("TestCookie",$_REQUEST["username"] . " " . $_REQUEST["password"], time()+3600*24, "/", "subd.mydomain.com");
}
if ($_GET["what"] == "get")
{
var_dump($_COOKIE);
c#客户端代码:
var n = new ClassLibrary1.Class1();
await n.get("http://subd.mydomain.com/?what=set&username=foo&password=bar");
await n.get("http://subd.mydomain.com/?what=get");
下面是一个来自服务器的cookie响应示例
Set-Cookie: ARRAffinity=295612ca; Path=/;Domain=subd.mydomain.com
Set-Cookie: TestCookie=foo+bar; expires=Fri, 04-Jan-2013 17:19:25 GMT; path=/; domain=subd.mydomain.com
在Windows 8商店/Metro应用程序中,这是结果:
array(2) {
["ARRAffinity"]=>
string(8) "295612ca"
["TestCookie"]=>
string(7) "foo bar"
}
在Windows Phone应用程序中,结果如下:
array(0){
}
这样设置时,Windows Phone看不到任何cookie。
我改变TestCookie
是如何设置的,结果从服务器现在看起来像这样:
Set-Cookie: ARRAffinity=295612ca;Path=/;Domain=subd.mydomain.com
Set-Cookie: TestCookie=foo+bar; expires=Fri, 04-Jan-2013 17:29:59 GMT; path=/
TestCookie
现在不显式设置域,ARRAffinity不变。
Windows 8 Store/Metro应用程序现在返回这个:
array(2) {
["TestCookie"]=>
string(7) "foo bar"
["ARRAffinity"]=>
string(8) "295612ca"
}
Windows Phone 8应用程序返回如下:
array(1) {
["TestCookie"]=>
string(7) "foo bar"
}
ARRAffinity
cookie没有被发送,因为它显式地声明了一个域。
如果我分配一些断点并检查请求的CookieContainer
,我在m_domainTable
中有两个条目
+ [0] {[.subd.mydomain.com, System.Net.PathList]} System.Collections.Generic.KeyValuePair<string,System.Net.PathList>
+ [1] {[subd.mydomain.com, System.Net.PathList]} System.Collections.Generic.KeyValuePair<string,System.Net.PathList>
未发送的cookie位于.subd.mydomain.com
容器中。这在Windows 8和Windows Phone 8上都是一样的。
但是,如果cookie像这样声明自己:
Set-Cookie: TestCookie=foo+bar; expires=Fri, 04-Jan-2013 17:19:25 GMT; path=/; domain=.mydomain.com
在Windows Phone 8上正确发送。
在我的原始情况下,服务器声明cookie的方式相同,无论它是通过mydomain.com或www.mydomain.com访问;但Windows Phone 8似乎不认为"。mydomain.com"的cookie应该被发送到"mydomain.com"。这是有问题的,因为即使服务器将"subd.mydomain.com"作为域,它也被视为前面有一个点,然后由于它自己的错误而无法工作。它似乎不发送域名信息与cookie有它的正确处理。
这是一个已知的问题,反馈文章在这里
目前没有太多进展,但最近才提交。我只能建议对这个bug进行投票,并定期检查它的状态。如果你不能等待,请联系微软技术支持,以便尽快跟进。