C# .NET - Login using WebRequest and WebResponse class in c#

Asked By MARKAND BHATT on 23-Aug-13 07:06 AM

Hello all I having an issue in logging into a website using webRequest and WebResponse.
I thought let me reach out to experts by posting a question.
I am trying to login in http://www.mbhatt.in/ and want to access the page after the login is successfull.
I used following code to do so. But somehow it failed.

01.string poststring = string.Format("Username={0}&Password={1}", Username, Password);
02.byte[] postdata = Encoding.UTF8.GetBytes(poststring);
03.HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
04.webRequest.Method = "POST";
05.webRequest.ContentType = "application/x-www-form-urlencoded";
06.webRequest.ContentLength = postdata.Length;
07.webRequest.Credentials = new NetworkCredential(Username, Password);
08.Stream writer = webRequest.GetRequestStream();
09.writer.Write(postdata, 0, postdata.Length);
10.HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
11.cookies.Add(webResponse.Cookies);

I dont know what to do when cookie gets added to my browzer. How can i use this cookie to access the other pages of that website after loging in. I want to do this using WebRequest and WebResponse