I need to be able to append an HTTP header (or modify the user agent string) sent by most HTTP requests leaving a PC.
By most, I mean anything inside Internet Explorer, as well as anything coming from a .NET application.
I've already acoomplished the Internet Explorer side of things by writing a BHO, but that BHO won't intercept requests made by ClickOnce controls loaded into IE, which is another requirement.
The .NET applications in my case are all using WebRequest.Create to made their requests.
Is this possible? I'm hoping I get inject some code into the System.Net stack someplace.
A proxy was one possibility, but it has proven difficult to create a proxy that doesn't perform like hell. HTTPS traffic is another problem.
Ok. I figured this out.
I created a custom web request module that explicitly sets the user agent of the HttpWebRequest before it's returned by the WebRequest.Create factory.
First, create a class that implemented IWebRequestCreate:
You'll need to sign this assembly and add it to the GAC.
Now in the machine.config on your machine, add the following configuration section:
Now whenever somebody calls WebRequest.Create, they'll get an HttpWebRequest with the user agent string already set.
I also attempted to create a custom class that inherited from HttpWebRequest, but this was tricky because there was no default public constructor. The only public contructor was an obsolete implementation of ISerializable.
I successfully got my dervied class to be used with the ISerializable constructor, but the resulting "pseudo-hydrated" object wasn't in a valid state, likely due to the fact the ISerializable implementation is obsolete and hasn't been maintained by Microsoft.
Still, it's possible that one could make this work if they investigate the errors encountered when using it in a bit more detailed. Specifically, there are issues with ServicePoint related access. Using reflection, one might be able to get the thing working. Here is my implementation for reference: