Re-Sending Received Cookies with Subsequent HTTP GET’s

I’ve read information on your site about how to get the content at a URL and save it as an XML document. Below is a portion of the code to give you an idea of what I am referring to.

String html = http.QuickGetStr(http://www.hocuspocus.com);

htmlToXml.WriteScriptToFile(xml, “out.xml”, “iso-8859-1”);

My question is this: What if one cannot directly access the URL in the QuickGetStr method?

For instance, some web sites that contain a page of information I want access to, first require me to visit a home page. Although I do not have to log on, I cannot simply enter the URL of the page into the browser and access the page. I must first go to the home page and then navigate to the page I want. In C#, I must get the response from the home page and then add its cookies to my request for the second page. Is this making sense?

How can this situation be handled with the Chilkat HTTP library?

Answer:

It’s very easy. Set these properties:

http.SendCookies = true;
http.SaveCookies = true;
http.CookieDir = "memory";

Then call QuickGetStr for the page that must be accessed first to get the cookies. Then call QuickGetStr for the page you need. Any cookies received for the 1st page will be sent with the HTTP request for subsequent pages (for the same domain, according to all cookie rules and such…)

Tags :