Cookie不能在Windows Phone应用程序发送的,但饼干在Windows 8的应用程序发送的相同code [英] Cookies not sent on Windows Phone app, but cookies are sent with same code in Windows 8 app

查看:124
本文介绍了Cookie不能在Windows Phone应用程序发送的,但饼干在Windows 8的应用程序发送的相同code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的类,使得使用GET和POST请求的HttpWebRequest / HttpWebResponse

I have a basic class that makes GET and POST requests using HttpWebRequest/HttpWebResponse.

我用我的课登录到一个API,然后请求数据。在Windows 8的新城的应用程序,它完全按预期工作。在Windows Phone 8的应用程序,登录看似成功,但在随后的数据请求,没有cookie发送和服务器的响应,如果没有记录在客户端。

I use my class to login to an API and then request data. In a Windows 8 "Metro" application, it works exactly as expected. On a Windows Phone 8 application, the login appears to succeed, but in the subsequent request for data, no cookies are sent and the server responds as if the client is not logged in.

下面是类,这完全一样的code采用的是Windows 8的应用程序和Windows Phone应用:

Here is the class, this exact same code is used in the Windows 8 app and the Windows Phone app:

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);
        }
    }

而code发起的请求:

And the code to initiate the requests:

    var n = new Class1();
    await n.post("https://mydomain.com/api/login/", "username=myusername&password=mypassword");
    await n.get("https://mydomain.com/reader/feeds/");

好奇的是,如果我preFIX域名与WWW。它工作在Windows Phone 8的应用程序和Windows 8地铁应用两者。

The curious thing is that if I prefix the domain name with "www." it works in both the Windows Phone 8 app and the Windows 8 Metro app.

我认为这是与域是如何处理的。 cookie的域名是.mydomain.com,并没有preFIX一定觉得cookie的不属于该域。经过一番搜索,我发现有人注意到类似的问题的报告。

I think it has something to do with how the domain is handled. The cookie's domain is ".mydomain.com", and without the prefix it must think the cookie's do not belong to that domain. After some searching I found a report of someone noticing a similar problem.

我不明白的是为什么这是区别对待比在Windows Phone应用程序在Windows 8的应用程序,因此该行换行相同的code工作在一个平台上,但无法在另一个之上。

What I do not understand is why this is treated differently in the Windows 8 app than the Windows Phone app, so that line-for-line identical code works on one platform but fails on another.

我已经做了一些挖掘到这一点。

I have done some more digging into this.

服务器code我用,这是在PHP中:

The server code I used for this is in 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);

客户端code在C#:<​​/ P>

Client code in 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响应的例子

Here is an example of a cookie response from the server

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商店/地铁的应用程序,这是结果:

In the Windows 8 Store/Metro app, this is the result:

array(2) {
  ["ARRAffinity"]=>
  string(8) "295612ca"
  ["TestCookie"]=>
  string(7) "foo bar"
}

在Windows Phone的应用程序,这是结果:

In the Windows Phone app, this is the result:

array(0){
}

在Windows Phone并没有看到任何cookie时,他们设置这个样子。

The Windows Phone does not see any cookies when they are set this way.

我改怎么 TestCookie 设置,从服务器的结果现在看起来是这样的:

I change how TestCookie is set, the result from the server now looks like this:

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是不变的。

TestCookie now does not explicitly set a domain, ARRAffinity is unchanged.

在Windows 8商店/地铁的应用程序现在返回的:

The Windows 8 Store/Metro app now returns this:

array(2) {
  ["TestCookie"]=>
  string(7) "foo bar"
  ["ARRAffinity"]=>
  string(8) "295612ca"
}

在Windows Phone 8的应用程序,返回这样的:

The Windows Phone 8 app, returns this:

array(1) {
  ["TestCookie"]=>
  string(7) "foo bar"
}

,因为它明确声明了域的 ARRAffinity 的cookie不会被发送。

The ARRAffinity cookie is not sent because it explicitly declares a domain.

如果我给你一些断点和检查请求的的CookieContainer ,我在 m_domainTable <两个条目/ P>

If I assign some breakpoints and check the CookieContainer of the request, I have two entries in the 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>

这是不是送的饼干,是在 .subd.mydomain.com 容器。这是在同一个在Windows 8和Windows Phone 8。

The cookie that isn't sent, is in the .subd.mydomain.com container. This is the same on both Windows 8 and Windows Phone 8.

然而,如果cookie声明自己是这样的:

However, if the cookie declares itself like this:

Set-Cookie: TestCookie=foo+bar; expires=Fri, 04-Jan-2013 17:19:25 GMT; path=/; domain=.mydomain.com

这是正确的Windows Phone 8的发送。

It is correctly sent on Windows Phone 8.

在我原来的情况下,服务器声明的cookie以同样的方式,无论它是否是通过mydomain.com和www.mydomain.com访问的;作为.mydomain.com,但Windows Phone 8的似乎并不想一个cookie的.mydomain.com应该被发送到mydomain.com。这是有问题的,因为即使服务器把subd.mydomain.com作为域,它被视为具有preceding点,然后通过无断层其自身的不工作。现在看来,这已经不是发送域信息的cookie来有它正确对待。

In my original case, the server declares the cookie the same way regardless of if it is accessed via mydomain.com or www.mydomain.com; as ".mydomain.com", but Windows Phone 8 doesn't seem to think a cookie for ".mydomain.com" should be sent to "mydomain.com". This is problematic, as even if the server puts "subd.mydomain.com" as the domain, it is treated as having a preceding dot, and then doesn't work through no fault of its own. It seems it has to not send domain info with the cookie to have it treated correctly.

推荐答案

这是一个已知的问题,反馈文章<一个href="https://connect.microsoft.com/VisualStudio/feedback/details/771651/cookiecontainer-subdomain-handling-issue-in-net-4-0">is这里。

This is a known issue, the feedback article is here.

没有太多的事情,但现在有人提出相对较晚。我只能建议投票支持这个bug,并定期检查回来的地位。如果你不能等待请联系微软支持一个可能快速跟进。

Not much going on right now but it was filed relatively recently. I can only recommend voting for this bug and periodically checking back for its status. If you can't afford to wait then contact Microsoft Support for a possibly quicker follow-up.

这篇关于Cookie不能在Windows Phone应用程序发送的,但饼干在Windows 8的应用程序发送的相同code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆