0

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.

Grimbstik
  • 1
  • 4
  • In answer to your HTTPOnly question, temporarily turn off HTTPOnly and you will have your answer. – Reid Apr 07 '15 at 13:50
  • @Reid how can I turn off the httponly ? I thought that httponly was a flag that can't be removed from a cookie. – Grimbstik Apr 07 '15 at 14:06
  • Unless I am mistaken, look at your web.config: httpOnlyCookies="true" It would seem to me that if it were set to false, that it would no longer be HTTP only. https://msdn.microsoft.com/en-us/library/system.web.configuration.httpcookiessection.httponlycookies(v=vs.110).aspx – Reid Apr 07 '15 at 21:09
  • @Reid, I can't use web.config because it's not a webapp. I looked for doing this in code but it seems to be impossible. I've found a solution for WP8 (http://stackoverflow.com/questions/15560653/preserving-httponly-cookies-on-windows-phone?answertab=votes#tab-top) but it doesn't work on WP7. – Grimbstik Apr 08 '15 at 07:54

1 Answers1

0

I finally found why it doesn't work : I forgot to declare the cookiecontainer of the HttpWebRequest. So I use a local field to save the CookieContainer and reuse it for each call to RequestPOST() I do.

    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. The CookieContainer still show no cookies because HTTPOnly cookies are not referenced in the container but they are in it anyway.

I hope it will help.

Grimbstik
  • 1
  • 4