[英]Cannot get an httponly cookie with HttpWebResponse on windows phone 7 app
I'm developing an app using C# for a project and I need to get a cookie after a POST request on a website. 我正在为项目开发使用C#的应用程序,并且在网站上进行POST请求后需要获取Cookie。 I'm using HttpWebResponse to get the result of my request. 我正在使用HttpWebResponse来获取请求的结果。 My problem is that the CookieCollection is empty and I don't know why. 我的问题是CookieCollection为空,我不知道为什么。 Is it possible that the cookie doesn't appear because it's an HTTPOnly cookie ? 该cookie是否可能因为它是HTTPOnly cookie而没有出现?
Here is my code for the entire POST request : 这是我的整个POST请求的代码:
private void RequestPOST(string uri)
{
Uri myUri = new Uri(uri);
HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(myUri);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
Debug.WriteLine("RequestStream : BEGIN");
myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);
}
private void GetRequestStreamCallback(IAsyncResult callbackResult)
{
HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
Stream postStream = myRequest.EndGetRequestStream(callbackResult);
byte[] byteArray = Encoding.UTF8.GetBytes(this._postData);
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), myRequest);
}
private void GetResponsetStreamCallback(IAsyncResult callbackResult)
{
HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
CookieCollection cookies = response.Cookies;
using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
{
this._retourPost = httpWebStreamReader.ReadToEnd();
Debug.WriteLine(cookies.Count);//My problem appears here, cookieCount throws a NullException
foreach (Cookie cook in response.Cookies)
{
Debug.WriteLine(cook.Name + " : "+ cook.Value);
}
}
Debug.WriteLine("END");
}
I know that there already are some similar questions but I still can't make my application works. 我知道已经有一些类似的问题,但是我仍然无法使我的应用程序正常运行。
I hope my question is clear. 希望我的问题清楚。
Thank you. 谢谢。
I finally found why it doesn't work : I forgot to declare the cookiecontainer of the HttpWebRequest. 我终于找到了为什么它不起作用:我忘了声明HttpWebRequest的cookiecontainer。 So I use a local field to save the CookieContainer and reuse it for each call to RequestPOST() I do. 因此,我使用一个本地字段来保存CookieContainer,并在每次对RequestPOST()的调用中重用它。
private CookieContainer cookiecontainer = new CookieContainer();
private void RequestPOST(string uri)
{
Uri myUri = new Uri(uri);
HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(myUri);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.CookieContainer = this.cookiecontainer;
Debug.WriteLine("RequestStream : BEGIN");
myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);
}
Actually I don't have to read the HTTPOnly cookie, I just need to have it. 实际上,我不必阅读HTTPOnly cookie,我只需要拥有它。 The CookieContainer still show no cookies because HTTPOnly cookies are not referenced in the container but they are in it anyway. CookieContainer仍然不显示cookie,因为在容器中未引用HTTPOnly cookie,但它们仍然存在于其中。
I hope it will help. 希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.