I'm trying to make a HttpWebRequest from c# winapp.
I have got the following request headers from google chrome developers tool (omitted unnecessary parts)-
Cookie: PHPSESSID=f530kslitnsq7uho33auebkd11;ciIPPBXOBd=TOAf5yvdIlHf7Ak%3D
My code is -
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("example.com");
Cookie cookie = new Cookie("PHPSESSID", "f533auebkd11") { Domain = "example.com" };
request.CookieContainer.Add(cookie);
But I am getting object reference null error in the last line.
How to get rid of?
by default cookie container is null. try this.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.google.com");
Cookie cookie = new Cookie("PHPSESSID", "f533auebkd11") { Domain = "example.com" };
//if cookie contaner is null then try new container
request.CookieContainer = request.CookieContainer ?? new CookieContainer();
request.CookieContainer.Add(cookie);