locked
Where are thr POST response cookies? RRS feed

  • Question

  • Hi.
    I have this code:

     

    CookieContainer cookieJar = new CookieContainer();
    req = (HttpWebRequest)WebRequest.Create(Link1);
    req.UserAgent = "Mozilla/5.0";
    req.Accept = "text/html";
    req.CookieContainer = cookieJar;
    // COOKIES ARE HERE - OK
    res = (HttpWebResponse)req.GetResponse();
    
    dataStream = res.GetResponseStream();
    StreamReader readStream = new StreamReader(dataStream);
    html = readStream.ReadToEnd();
    
    // SECOND STAGE
    req = (HttpWebRequest)WebRequest.Create(Link1);
    send = Encoding.UTF8.GetBytes("agreed=" + agreed + "&sid=" + sid);
    req.Method = "POST";
    req.ContentLength = send.Length;
    req.ContentType = "application/x-www-form-urlencoded";
    req.UserAgent = "Mozilla/5.0";
    req.CookieContainer = cookieJar;
    // COOKIES SENT TO SERVER - OK
    
    dataStream = req.GetRequestStream();
    dataStream.Write(send, 0, send.Length);
    
    res = (HttpWebResponse)req.GetResponse();
    // HOW TO GET COOKIES FROM THE SERVER?
    

     

    As you can see in last part of the code, I have a POST request.
    The request does transfer the cookies to the server,
    but does not get anything back into the cookieJar.

    If I change the last line of the req in the 2nd stage to:
    req.CookieContainer = new CookieContainer()
    then, I do get the cookies of the response, but no cookies are being sent.

    What can I do in order to send and get back the cookies?

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Moved by Aspen VJ Wednesday, April 13, 2011 5:36 AM (From:Visual C# General)
    Tuesday, April 5, 2011 11:08 AM

All replies

  • CookieContainer cc;
    req.CookieContainer = cc;
    HttpWebResponse response
    = (HttpWebResponse)req.GetResponse();
    cc.Add(response.Cookies);


    family as water
    Tuesday, April 5, 2011 12:49 PM
  • Do i need to use a new cookiecontainer?

    I tried to put your code in my code by adding one line after the response:

    cookieJar.Add(res.Cookies)

    But no cookies were accepted. I checked it during debugging,

    res.Cookies has 0 cookies. Why?

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    Tuesday, April 5, 2011 1:45 PM
  • or try:

    string url="www.xxx.com";

    CookieContainer cc;

    Uri uri = new Uri(url);

    CookieCollection c=cc.GetCookies(uri);

    Cookie ck = new Cookie();

    ck =c[0];

     


    family as water
    Wednesday, April 6, 2011 2:20 AM
  • Hi Noam,

    Based on your description, I think your issue is more about asp.net. So I think it is better for you to post it in that forum. As there are more asp.net experts there.

    Here is the link: http://forums.asp.net/ 

    Thanks for understanding and supporting.


    Vin Jin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, April 7, 2011 2:56 AM