我对这里的问题感到非常困惑。我对CookieContainer
的理解一定是完全错误的。我正在尝试登录我机器上托管的网站。我把小提琴设置为代理,所以我能够读取流量。
每当我尝试在Windows手机上查看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手机代码都没有获得这些cookie。这与这个问题几乎相同:Cannot get cookies in wp7 using HttpWebRequest但是我不明白解决方案是什么。
答案 0 :(得分:1)
HttpOnly
,因此cookie容器根本不会暴露它们,即使您要求计算cookie数。
奇怪的是,在控制台版本中,我可以看到有两个,但我认为Windows Phone的System.Net.dll锁定了CookieContainer
多一点。