C # Get Cookies from server response provided in POST data

I can’t solve this problem (login): 1) Publish some data 2) The server responds and generates a response with some cookies in the headers (Set-Cookies) 3) I want to save these cookies in order to subsequently use them to generate more queries.

My C # code is as follows:

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 :( 

The fact is that WebResp has a "Set-Cookie" header with values. Before I start and analyze the headers, I wonder why the cookie does not propagate (0 cookies) and is filled with the values ​​from the header.

Anyone have an idea?

+6
c # post cookies
source share
1 answer

Because you need to use a cookie container .

 var cookieContainer = new CookieContainer(); WebReq.CookieContainer = cookieContainer; 
+8
source share

All Articles