Daily Deeds: Using CookieContainer in WebClient Class

Recently I decided to put stuff out on web that I actually face everyday in my part-time endeavors. I would like to keep these precise so I can write these fast enough. Smile

First of all, I’m a noob so kindly go easy on me. The point I’m putting out here is found when I was doing a pretty simple job of writing a method that checks whether a web url is valid or not. Easy at it seems the only nightmare was getting “too many automatic redirection” exception when the site seems okay in browser. I used firebug/fiddler to debug the HTTP get request  And voila! Here is the solution.

The most probable reason of this problem might be these:

1. You might need to set webRequest.AllowAutoRedirect = false where webRequest is a HttpWebRequest object

2. You might be getting redirected just because your request is missing a cookie container.

But the problem arises when Im using the WebClient class, what would I do? The solution is I’m gonna override the WebClient class and get the inner HttpWebRequest Object to make some subtle changes along with allotting a CookieContainer object. Smile

So, here’s the overridden class looks like:

public class WebClientEx : WebClient

private readonly CookieContainer m_container = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
WebRequest request = base.GetWebRequest(address);
HttpWebRequest webRequest = request as HttpWebRequest;
if (webRequest != null)
webRequest.CookieContainer = m_container;
webRequest.AllowAutoRedirect = false;
return request;

And using the new class is never harder than this, Smile

using (var client = new WebClientEx())

// fine, no content downloaded
client.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0";
client.Headers[HttpRequestHeader.Accept] = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

string s1 = client.DownloadString(url);

return url+" "+"exists";
catch (Exception ex)

return url+" has exception :"+ ex.Message;

And voila! It works! Hope this helps to those people who spends days on stackoverflow like me Smile


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s