我对这里的问题感到非常困惑。我的理解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但是我不明白解决方案是什么。