Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CefSharp 3 set proxy at Runtime

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".

like image 775
Aladdin Avatar asked Mar 18 '16 22:03

Aladdin


2 Answers

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=""
    });
like image 74
Aladdin Avatar answered Sep 28 '22 05:09

Aladdin


if anyone need any other soulition i found this solution.

Cef.UIThreadTaskFactory.StartNew(delegate
        {
            string ip = "ip or adress";
            string port = "port";
            var rc = this.browser.GetBrowser().GetHost().RequestContext;
            var dict = new Dictionary<string, object>();
            dict.Add("mode", "fixed_servers");
            dict.Add("server", "" + ip + ":" + port + "");
            string error;
            bool success = rc.SetPreference("proxy", dict, out error);

        });
like image 31
FreeCLoud Avatar answered Sep 28 '22 03:09

FreeCLoud