我有一个基本类,它使用以下命令发出 GET 和 POST 请求HttpWebRequest
/HttpWebResponse
.
我使用我的类登录 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 不属于该域。经过一番搜索我发现有人注意到类似问题的报告 http://blog.codeblack.nl/post/HttpWebRequest-HttpWebResponse-and-cookies.aspx.
我不明白的是为什么 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 Store/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"
}
The 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 上可以正确发送。
在我原来的情况下,无论是通过 mydomain.com 还是 www.mydomain.com 访问,服务器都以相同的方式声明 cookie;作为“.mydomain.com”,但 Windows Phone 8 似乎不认为“.mydomain.com”的 cookie 应该发送到“mydomain.com”。这是有问题的,因为即使服务器将“subd.mydomain.com”作为域,它也会被视为具有前面的点,然后由于其自身的错误而无法工作。看来它必须不使用 cookie 发送域信息才能正确处理它。