locked
Session cookies not working RRS feed

  • Question

  • Hello !

    My program uses HttpWebRequest several times to login on site page, and problem is - i need to use session cookies to indentity all my requests are from same program. What i'm trying to do is i'm saving cookies send by site:

    Dim MySavedCookies as System.Net.CookieCollection

    MySavedCookies = MyHttpWebRequest1.Cookies

    And on other accesses i'm sendng this cookies collection back, and updating cookies collection:

    Dim myHttpWebRequest2 as System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(SomeUri)

    myHttpWebRequest2.CookieCOntainer=new System.Net.CookieContainer

    myHttpWebRequest2.CookieContainer.Add(Cookies)

    .......

    MySavedCookies.Add(myHttpWebResponse2.Cookies

     

    And this is not working. As far as i understand what i need to do is to save cookies i got in responses and send them back with each request i send, so site will know that's me again, but site don't recognize me, i don't know why, is there something wrong i'm doing here?

    Friday, September 15, 2006 11:17 PM

Answers

  • Below is a snippet that shows how to ensure any cookies reponse by an initial http request are transmitted (if appropriate) when subsequent requests are made:

    CookieContainer container = new CookieContainer();

    //request #1

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://asdf");

    req.CookieContainer = container;

    HttpWebResponse resp = req.GetResponseStream();

    //output all the cookies that were returned

    if (resp.Cookies != null && resp.Cookies.Count != 0)

    {

    foreach (Cookie c in resp.Cookies)

    {

    Console.WriteLine("\t" + c.ToString());

    }

    }

    HttpWebRequest req2 = (HttpWebRequest)WebRequest.Create("http://asdf");

    req2.CookieContainer = container;

    HttpWebResponse resp2 = req.GetResponseStream();

    //output all the cookies that were returned

    if (resp2.Cookies != null && resp2.Cookies.Count != 0)

    {

    foreach (Cookie c in resp2.Cookies)

    {

    Console.WriteLine("\t" + c.ToString());

    }

    }

     

    Some debugging steps you may want to follow are:

    - Are the cookies you expect to be receiving actually being received?  If yes, is the cookie data correct?

    - Are you using the same cookie container accrosss requests?

    - Validate the cookies you expect to be sent in subsequent requests are actually being sent by looking at a network trace (ie. use netmon or ethereal)

    Saturday, September 16, 2006 4:25 AM

All replies

  • Below is a snippet that shows how to ensure any cookies reponse by an initial http request are transmitted (if appropriate) when subsequent requests are made:

    CookieContainer container = new CookieContainer();

    //request #1

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://asdf");

    req.CookieContainer = container;

    HttpWebResponse resp = req.GetResponseStream();

    //output all the cookies that were returned

    if (resp.Cookies != null && resp.Cookies.Count != 0)

    {

    foreach (Cookie c in resp.Cookies)

    {

    Console.WriteLine("\t" + c.ToString());

    }

    }

    HttpWebRequest req2 = (HttpWebRequest)WebRequest.Create("http://asdf");

    req2.CookieContainer = container;

    HttpWebResponse resp2 = req.GetResponseStream();

    //output all the cookies that were returned

    if (resp2.Cookies != null && resp2.Cookies.Count != 0)

    {

    foreach (Cookie c in resp2.Cookies)

    {

    Console.WriteLine("\t" + c.ToString());

    }

    }

     

    Some debugging steps you may want to follow are:

    - Are the cookies you expect to be receiving actually being received?  If yes, is the cookie data correct?

    - Are you using the same cookie container accrosss requests?

    - Validate the cookies you expect to be sent in subsequent requests are actually being sent by looking at a network trace (ie. use netmon or ethereal)

    Saturday, September 16, 2006 4:25 AM
  • Thanks a lot, problem solved !!!
    Saturday, September 16, 2006 8:32 AM