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:
public class CustomHttpRequestCreator : IWebRequestCreate
{
public CustomHttpRequestCreator(){}
public WebRequest Create(Uri uri)
{
HttpWebRequest webRequest = Activator.CreateInstance(typeof(HttpWebRequest),
BindingFlags.CreateInstance | BindingFlags.Public |
BindingFlags.NonPublic | BindingFlags.Instance,
null, new object[] { uri, null }, null) as HttpWebRequest;
webRequest.UserAgent = "OMG IT WORKED!";
return webRequest;
}
}
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:
<system.net>
<webRequestModules>
<remove prefix="http:"/>
<remove prefix="https:"/>
<add prefix="http:" type="HttpWebRequestTest.CustomHttpRequestCreator, HttpWebRequestTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4ba7a6b9db5020b7" />
<add prefix="https:" type="HttpWebRequestTest.CustomHttpRequestCreator, HttpWebRequestTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4ba7a6b9db5020b7" />
</webRequestModules>
</system.net>
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:
public class CustomHttpWebRequest : HttpWebRequest
{
public CustomHttpWebRequest(SerializationInfo serializationInfo, StreamingContext streamingContext) : base(serializationInfo, streamingContext) { }
internal CustomHttpWebRequest(Uri uri) : base(BuildSerializationInfo(uri), new StreamingContext())
{
this.UserAgent = "OMG IT WORKED! (Constructor)";
}
private static SerializationInfo BuildSerializationInfo(Uri uri)
{
HttpWebRequest webRequest = Activator.CreateInstance(typeof(HttpWebRequest),
BindingFlags.CreateInstance | BindingFlags.Public |
BindingFlags.NonPublic | BindingFlags.Instance,
null, new object[] { uri, null }, null) as HttpWebRequest;
var serializationInfo = new SerializationInfo(typeof(HttpWebRequest), new System.Runtime.Serialization.FormatterConverter());
((ISerializable)webRequest).GetObjectData(serializationInfo, new StreamingContext());
return serializationInfo;
}
public override WebResponse GetResponse()
{
this.UserAgent = "OMG IT WORKED!";
return base.GetResponse();
}
public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state)
{
this.UserAgent = "OMG IT WORKED ASYNC!";
return base.BeginGetResponse(callback, state);
}
}