0

我对这里的问题感到非常困惑。我的理解CookieContainer一定是完全错误的。我正在尝试登录我机器上托管的网站。我已将提琴手设置为代理,因此我可以读取流量。

每次我尝试在 windows phone 上查看 cookie 时,cookie 容器总是空的。我尝试了很多不同的方法来装满那个容器,但一切都失败了。这是我尝试的最后一件事:

        CookieContainer cc = new CookieContainer();
        HttpWebRequest request = WebRequest.CreateHttp("http://localhost:8888/account/logon");
        request.ContentType = "application/x-www-form-urlencoded";
        request.Method = "POST";
        request.CookieContainer = cc;
        request.BeginGetRequestStream(streamResult =>
        {
            HttpWebRequest xR = streamResult.AsyncState as HttpWebRequest;
            Stream stream = xR.EndGetRequestStream(streamResult);
            StreamWriter sw = new StreamWriter(stream);
            sw.Write("username=user1&password=1111");
            sw.Close();

            xR.BeginGetResponse(responseResult =>
            {
                HttpWebRequest yR = responseResult.AsyncState as HttpWebRequest;
                HttpWebResponse response = yR.EndGetResponse(responseResult) as HttpWebResponse;
                response.Close();

                Dispatcher.BeginInvoke(() => MessageBox.Show("cc count: " + cc.Count.ToString() + "\nresponse count: " + response.Cookies.Count));
            }, xR);

        }, request);

但是,如果我在控制台应用程序中使用完全相同的代码,则容器“cc”具有 cookie!

        CookieContainer cc = new CookieContainer();
        HttpWebRequest request = WebRequest.CreateHttp("http://localhost:8888/account/logon");
        request.ContentType = "application/x-www-form-urlencoded";
        request.Method = "POST";
        request.CookieContainer = cc;
        request.BeginGetRequestStream(streamResul =>
        {
            HttpWebRequest xR = streamResul.AsyncState as HttpWebRequest;
            Stream stream = xR.EndGetRequestStream(streamResul);
            StreamWriter sw = new StreamWriter(stream);
            sw.Write("username=user1&password=1111");
            sw.Close();

            xR.BeginGetResponse(responseResult =>
            {
                HttpWebRequest yR = responseResult.AsyncState as HttpWebRequest;
                HttpWebResponse response = yR.EndGetResponse(responseResult) as HttpWebResponse;
                response.Close();

                Console.WriteLine("cc count: {0}\nresponse count: {1}", cc.Count.ToString(), response.Cookies.Count);
            }, xR);

        }, request);

        Console.Read();

每次测试时,服务器都会发回我正在寻找的两个 cookie。然而,无论出于何种原因,windows phone 代码都无法获取这些 cookie。这与这个问题几乎相同:Cannot get cookies in wp7 using HttpWebRequest但是我不明白解决方案是什么。

4

1 回答 1

1

哇,我觉得自己像个傻瓜。它一直在工作......也许对其他人来说这是显而易见的,但我错过了。问题是,由于被发回HttpOnly的 cookie 是 ,cookie 容器根本不会公开它们,即使您询问 cookie 的计数。

奇怪的是,虽然在控制台版本中,我可以看到有两个,但我想 Windows phone 的 System.Net.dll 锁定了CookieContainer一点。

于 2012-12-19T00:13:56.597 回答