by jfisch
28. July 2009 18:47
I've been bothered by being constrained by the header validation within the HttpWebRequest object several times. The constraints, IMO, are a little too constrictive on what can and can't be added to the HttpWebRequest object. Here's a solution. I've implemented the methods as extension methods on the HttpWebRequest object. The first gives you internal access to the WebHeaderCollection object that the HttpWebRequest provides access to, the second HeadersRaw provides completely uninhibited access to the header collection via the WebHeaderCollection object's internal NameValueCollection.
I haven't done a great deal of testing on this particular solution, but it should be pretty straight forward to utilize. I'm a little weary of potential threading issues given that I've implemented this as an extension method and what used to be fields of an object are now static fields. So, do some testing if you're planning on using this in a multi-threaded scenario. To utilize, you'll want to add the WebRequest namespace (or whatever you rename it as) to your sourcecode, then you'll have access to the raw header objects via the following:
WebHeaderCollection header1 = request.HeadersUnAuthenticated();
or
NameValueCollection header2 = request.HeadersRaw();
WebRequest.zip (3.30 kb)
Good luck!
Jeff