无法在 windows phone 7 应用程序上使用 HttpWebResponse 获取 httponly cookie
Cannot get an httponly cookie with HttpWebResponse on windows phone 7 app
我正在使用 C# 为项目开发应用程序,我需要在网站上收到 POST 请求后获取 cookie。我正在使用 HttpWebResponse 来获取请求的结果。我的问题是 CookieCollection 是空的,我不知道为什么。 cookie 是否可能因为它是 HTTPOnly cookie 而没有出现?
这是我对整个 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");
}
我知道已经有一些类似的问题,但我仍然无法使我的应用程序正常工作。
我希望我的问题很清楚。
谢谢。
我终于找到了它不起作用的原因:我忘记声明 HttpWebRequest 的 cookiecontainer。所以我使用一个本地字段来保存 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);
}
其实我不需要读取HTTPOnly cookie,我只需要拥有它。 CookieContainer 仍然没有显示任何 cookie,因为容器中未引用 HTTPOnly cookie,但无论如何它们都在其中。
希望对您有所帮助。
我正在使用 C# 为项目开发应用程序,我需要在网站上收到 POST 请求后获取 cookie。我正在使用 HttpWebResponse 来获取请求的结果。我的问题是 CookieCollection 是空的,我不知道为什么。 cookie 是否可能因为它是 HTTPOnly cookie 而没有出现?
这是我对整个 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");
}
我知道已经有一些类似的问题,但我仍然无法使我的应用程序正常工作。
我希望我的问题很清楚。
谢谢。
我终于找到了它不起作用的原因:我忘记声明 HttpWebRequest 的 cookiecontainer。所以我使用一个本地字段来保存 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);
}
其实我不需要读取HTTPOnly cookie,我只需要拥有它。 CookieContainer 仍然没有显示任何 cookie,因为容器中未引用 HTTPOnly cookie,但无论如何它们都在其中。
希望对您有所帮助。