0

I get a nullreference exception at the cookiecontainer.add(OC);

I dont know what i was doing wrong, as i was following an example from: Sending cookies using HttpCookieCollection and CookieContainer and I more or less was following it perfectly. The error occurs for both INDEX and KEY referencing to the cookie.

System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URI);
        request.KeepAlive = true;
        HttpCookieCollection cookieJar = Request.Cookies;
        //foreach (string cookieString in Request.Cookies)
        for(int i = 0; i < cookieJar.Count; i++)
        {
            System.Web.HttpCookie cookie = cookieJar.Get(i);
            Cookie oC = new Cookie();
            oC.Domain = Request.Url.Host;
            oC.Expires = cookie.Expires;
            oC.Name = cookie.Name;
            oC.Path = cookie.Path;
            oC.Secure = cookie.Secure;
            oC.Value = cookie.Value;

            request.CookieContainer.Add(oC);
        }
Community
  • 1
  • 1
Fallenreaper
  • 10,222
  • 12
  • 66
  • 129

1 Answers1

8
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(oC);

A CookieContainer is null by default. You must assign a CookieContainer object to the property to have cookies returned in the Cookies property of the HttpWebResponse returned by the GetResponse method.-MSDN

Prabhu Murthy
  • 9,031
  • 5
  • 29
  • 36