Switch proxy dynamically

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Switch proxy dynamically

Postby gpenner » Fri Jan 19, 2018 1:46 pm

Hello everyone, I am just starting with CefSharp and I have a question that I wasn't able to find online.

Code: Select all
var cfsettings = new CefSettings();
_cfsettings.CefCommandLineArgs.Add("proxy-server", "IP:PORT");
_cfsettings.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36";
Cef.Initialize(_cfsettings);


This is how I am initializing my browser and the proxy works just fine, however, if I want to switch proxy dynamically I have no idea on how to do so, I tried to dispose Cef and start a new session but it won't let me do it, how should I proceed with that?
Thanks
gpenner
Newbie
 
Posts: 2
Joined: Fri Jan 19, 2018 1:41 pm

Re: Switch proxy dynamically

Postby gpenner » Sat Jan 20, 2018 1:40 pm

After reading the documentation (sorry I should've read that before posting here) I found the solution.
If the proxy is set by Cef settings we're unable to change it at run time, in order to make it work here's the solution that worked for me, just in case anyone else needs help with that.

Code: Select all
Cef.UIThreadTaskFactory.StartNew(delegate {
    var rc = Browser.GetBrowser().GetHost().RequestContext;
    var v = new Dictionary < string,
        object > ();
    v["mode"] = "fixed_servers";
    v["server"] = "IP:PORT";
    string error;
    bool success = rc.SetPreference("proxy", v, out error);
});
gpenner
Newbie
 
Posts: 2
Joined: Fri Jan 19, 2018 1:41 pm


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 83 guests