'Proxy with CefSharp

how can I set up a proxy that has username and password in CefSharp browser in visual basic? I tried with this code but doesn't work:

Dim proxy = "IP:PORT@USERNAME:PASSWORD"
Dim settings As New CefSettings()
settings.CefCommandLineArgs.Add("proxy-server", proxy)

Thanks



Solution 1:[1]

Your browser implement a IBrowser interface with GetHost method that allow you get RequestContext. You can set the proxy:

var requestContext = browser.GetHost().RequestContext;
var values = new Dictionary<string, object>
{
   ["mode"] = "fixed_servers",
   ["server"] = $"{proxyScheme}://{proxyHost}:{proxyPort}"
};

string error;
bool success = requestContext.SetPreference("proxy", values, out error);

To set user/password, you need to implement an IRequestHandler interface and implement this method:

public bool GetAuthCredentials(
    IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
{
    if (isProxy)
    {
        var browser2 = browserControl as IChromeRequestHandler;
        var proxyOptions = browser2?.ProxySettings;
        if (proxyOptions != null)
        {
            callback.Continue(proxyOptions.UserName, proxyOptions.Password);
            return true;
        }
    }

    callback.Dispose();
    return false;
}

Then, you must set the RequestHandler property of your browser:

browser.RequestHandler = new YourIRequestHandlerImplementation();

Sorry for the C# implementation, but I think may be useful to you.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Victor