Tuesday, September 29, 2009

Handling Cookies with WebRequest Class

WebRequest is an important class for establishing http connections programatically and fetching html page. You might well be aware about it's power. However I will be focussing on one limitation and a possible work-around.

While fetching some html content I hit upon a problem where it was expected for cookies to be enabled by the browser. Since I was making a hit using WebRequest class i was perplexed as to how to resolve it, untill better sense prevailed and I got the below solution.

Handling Cookies with WebRequest class

// Open a connection
HttpWebRequest WebRequestObject = (HttpWebRequest)HttpWebRequest.Create(Url);

// Set the Cookie Container under WebRequest object as a property
CookieContainer cookieJar = new CookieContainer();
WebRequestObject.CookieContainer = cookieJar;

// continue with your fetch

// A handy property is it seems that some page might take eternity to respond
WebRequestObject.Timeout = 5*60*1000; // (5 seconds) integer value in milliseconds