Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

.net - Add header to all* HTTP requests on a machine

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...