gpt4 book ai didi

c# - Windows Phone 应用程序不发送 Cookie,但在 Windows 8 应用程序中使用相同的代码发送 Cookie

转载 作者:可可西里 更新时间:2023-11-01 08:27:02 24 4
gpt4 key购买 nike

我有一个使用 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 不属于该域。经过一番搜索,我找到了 a report of someone noticing a similar problem .

我不明白为什么这在 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"
}

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 发送域信息才能正确处理它。

最佳答案

这是一个已知问题,反馈文章is here .

目前没有太多进展,但它是最近提交的。我只能建议为此错误投票并定期检查其状态。如果您等不及,请联系 Microsoft 支持以获得可能更快的跟进。

关于c# - Windows Phone 应用程序不发送 Cookie,但在 Windows 8 应用程序中使用相同的代码发送 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14108430/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com