I'm developing an app using C# for a project and I need to get a cookie after a POST request on a website. I'm using HttpWebResponse to get the result of my request. My problem is that the CookieCollection is empty and I don't know why. Is it possible that the cookie doesn't appear because it's an HTTPOnly cookie ?
Here is my code for the entire POST request :
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.