开发者

C# Get Cookies from server response provided on data POST

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();
S开发者_如何学GotreamReader _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 ?


Because you need to use a cookie container.

var cookieContainer = new CookieContainer();
WebReq.CookieContainer = cookieContainer;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜