KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm working on a project that involves some basic web crawling. I've been using HttpWebRequest and HttpWebResponse quite successfully. For cookie handling I just have one CookieContainer that I assign to HttpWebRequest.CookieContainer each time. I automatically gets populated with the new cookies each time and requires no additional handling from me. This has all been working fine until a little while ago when one of the web sites that used to work suddenly stopped working. I'm reasonably sure it's a problem with the cookies, but I didn't keep a record of the cookies from when it used to work so I'm not 100% sure. I've managed to simulate the issue as I see it with the following code: CookieContainer cookieJar = new CookieContainer(); Uri uri1 = new Uri("http://www.somedomain.com/some/path/page1.html"); CookieCollection cookies1 = new CookieCollection(); cookies1.Add(new Cookie("NoPathCookie", "Page1Value")); cookies1.Add(new Cookie("CookieWithPath", "Page1Value", "/some/path/")); Uri uri2 = new Uri("http://www.somedomain.com/some/path/page2.html"); CookieCollection cookies2 = new CookieCollection(); cookies2.Add(new Cookie("NoPathCookie", "Page2Value")); cookies2.Add(new Cookie("CookieWithPath", "Page2Value", "/some/path/")); Uri uri3 = new Uri("http://www.somedomain.com/some/path/page3.html"); // Add the cookies from page1.html cookieJar.Add(uri1, cookies1); // Add the cookies from page2.html cookieJar.Add(uri2, cookies2); // We should now have 3 cookies Console.WriteLine(string.Format("CookieJar contains {0} cookies", cookieJar.Count)); Console.WriteLine(string.Format("Cookies to send to page1.html: {0}", cookieJar.GetCookieHeader(uri1))); Console.WriteLine(string.Format("Cookies to send to page2.html: {0}", cookieJar.GetCookieHeader(uri2))); Console.WriteLine(string.Format("Cookies to send to page3.html: {0}", cookieJar.GetCookieHeader(uri3))); This simulates visiting two pages, both of which set two cookies. I
Tags (comma-separated)
Save Edits
Cancel