CefSharp 3 set proxy at Runtime

Aladdin picture Aladdin · Mar 18, 2016 · Viewed 12k times · Source

I downloaded CEF (chromuim embedded framework) binary distributation that comes with (cefclient & cefsimple) c++ examples, And Realized that cefclient can change proxy settings on run-time.

And the key to do that is to Grab the RequestContext and call the function SetPreference.

on CefClient all works just nice.

but on CefSharp calling SetPreference always returns false, and also HasPreference returns false for the preference name "proxy".

Answer

Aladdin picture Aladdin · Mar 19, 2016

thanks to amaitland the proper way to actively inforce changing the request-context prefrences, is to run the code on CEF UIThread as following:

    Cef.UIThreadTaskFactory.StartNew(delegate {
        var rc = this.browser.GetBrowser().GetHost().RequestContext;
        var v = new Dictionary<string, object>();
        v["mode"] = "fixed_servers";
        v["server"] = "scheme://host:port";
        string error;
        bool success = rc.SetPreference("proxy", v, out error);
        //success=true,error=""
    });