tags:

views:

19

answers:

1

Hi!

I can't get my head around this (login) problem: 1) I post some data 2) Server reacts and generate response with some cookies in headers (Set-Cookie) 3) I want to store that cookies so I can later use them to generate more requests

My C# code looks like this:

byte[] buffer = Encoding.ASCII.GetBytes(data_to_post);

HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);
WebReq.Method = "POST";
WebReq.ContentType = "application/x-www-form-urlencoded";
WebReq.ContentLength = buffer.Length;

Stream PostData = WebReq.GetRequestStream();
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();

HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
WebResp.Close(); // I want cookies here! But there is no cookies :(

Thing is that WebResp does have the "Set-Cookie" header with values. Before I will run and parse headers I wonder why Cookies object is not propagated (0 cookies) and filled with values from header.

Anyone have idea ?

A: 

Because you need to use a cookie container.

var cookieContainer = new CookieContainer();
WebReq.CookieContainer = cookieContainer;
Darin Dimitrov
ZOMG, youre right ... I added to WebReq.CookieContainer and received CookieCollection at WebResp... Silly of me... Thanks!
CookiezMonstar