HttpWebRequest的不是获取饼干 [英] Httpwebrequest not retrieving Cookies
问题描述
我已经创建了下面的代码,从而尽可能林意识到应该正常工作?它没有收到任何cookie的一切,我都用双丝鲨检查,正在返回的饼干......这是正在对Windows Phone 7的开发。
字节[] =内容GetLoginRequestContent(用户名,密码);
的CookieContainer的CookieContainer =新的CookieContainer();
HttpWebRequest的HttpWebRequest的=(HttpWebRequest的)WebRequest.Create(LoginUri);
httpWebRequest.ContentType = AuthContentType;
httpWebRequest.Method =POST;
httpWebRequest.Headers [引荐] = LoginRequestReferer;
httpWebRequest.CookieContainer =的CookieContainer;
httpWebRequest.Headers [HttpRequestHeader.ContentLength] = content.Length.ToString();
httpWebRequest.BeginGetRequestStream(async1 =>
{
使用(流流= httpWebRequest.EndGetRequestStream(async1))
流.WRITE(含量为0,content.Length);
httpWebRequest.BeginGetResponse(async2 =>
{
HttpWebResponse代表=(HttpWebResponse)httpWebRequest.EndGetResponse(async2);
CookieCollection饼干= rep.Cookies;使用(流流= rep.GetResponseStream())
使用(StreamReader的SR =新的StreamReader(流))
{
字符串contentX SR =
.ReadToEnd();
//如果等等等等
}
},NULL);
},NULL);
如果cookie将被标记的仅Http (通常是与会话cookie的情况下),您不能访问他们是客户端脚本出于安全原因。它被发送到客户端,客户端重新发送到服务器上的后续请求(如果它拥有一种饼干的容器),但你不能读取其在客户端上的价值。
I have created the following code, which as far as Im aware should work fine? It is not receiving any cookies at all, I have double checked with wire shark and the cookies are being returned... this is being developed on Windows Phone 7.
byte[] content = GetLoginRequestContent(username, password);
CookieContainer cookieContainer = new CookieContainer();
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(LoginUri);
httpWebRequest.ContentType = AuthContentType;
httpWebRequest.Method = "POST";
httpWebRequest.Headers["referer"] = LoginRequestReferer;
httpWebRequest.CookieContainer = cookieContainer;
httpWebRequest.Headers[HttpRequestHeader.ContentLength] = content.Length.ToString();
httpWebRequest.BeginGetRequestStream(async1 =>
{
using (Stream stream = httpWebRequest.EndGetRequestStream(async1))
stream.Write(content, 0, content.Length);
httpWebRequest.BeginGetResponse(async2 =>
{
HttpWebResponse rep = (HttpWebResponse)httpWebRequest.EndGetResponse(async2);
CookieCollection cookies = rep.Cookies;
using (Stream stream = rep.GetResponseStream())
using (StreamReader sr = new StreamReader(stream))
{
String contentX = sr.ReadToEnd();
//if blah blah
}
}, null);
}, null);
If the cookie is marked with HttpOnly (which is often the case with session cookies) you cannot access them it client side scripting for security reasons. It is sent to the client, the client resends it to the server (if it posses a cookie container) on subsequent requests, but you cannot read its value on the client.
这篇关于HttpWebRequest的不是获取饼干的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!