2

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.

RMD
  • 3,421
  • 7
  • 39
  • 85
  • There isn't really any other way other than a proxy. You say for *most* HTTP requests, what's the limitation? What requests *don't* you want to catch? What about other browsers (Firefox, Chrome, ...)? – Chris J Feb 09 '12 at 19:46
  • I explain what I mean by "most" in the second paragraph. – RMD Feb 09 '12 at 21:34
  • Okay - it wasn't clear that you meant only IE; just seemed odd to include IE, but not care about other browsers, so just checking that was by design, not accident. – Chris J Feb 09 '12 at 21:39
  • Nope. Not an accident. This is primarily for an internal application that only supports IE. – RMD Feb 10 '12 at 16:01
  • Did your CustomHttpRequestCreator intercept all traffic from IE and add that header? – user3088244 Dec 10 '13 at 19:09

1 Answers1

6

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);
    }
}
Calvin Fisher
  • 4,653
  • 5
  • 36
  • 47
RMD
  • 3,421
  • 7
  • 39
  • 85