Search code examples
c#webbrowser-controlgeckofx

Add http header to all requests to GeckoFx


I am trying to figure out a way to add a custom http header to all requests made through geckoFX. I tried to add a header using

GeckoMIMEInputStream strea=new GeckoMIMEInputStream();
strea.AddHeader("head","val"); 
geckoWebBrowser1.Navigate("http://google.com",GeckoLoadFlags.None,"",null,strea);

I need a way to add a custom header, to absolutely ALL requests made when going to a webpage. Thank you


Solution

  • You can implement your own observer (nsIObserver interface) to intercept the http-on-modify-request notification and there add headers.

    class Observer: nsIObserver
    {
        public void Observe(nsISupports aSubject, string aTopic, string aData)
        {
            var request = Xpcom.QueryInterface<nsIHTTPChannel>(aSubject);
            request.SetRequestHeader(name, value, merge);
        }
    }
    

    and register it

    Xpcom.GetService<nsIObserverService>("@mozilla.org/observer-service;1").AddObserver(new Observer(), "http-on-modify-request", false);