需要cefsharp代理验证

时间:2017-02-14 00:04:25

标签: c# proxy chromium-embedded cefsharp

我正在尝试在cefsharp上使用Auth代理 我尝试了这个代码,它只使用代理而不使用Auth 我该怎么做才能设置Auth。

Cef.UIThreadTaskFactory.StartNew(delegate
                    {
                        string ip = "IP";
                        string port = "PORT";
                        var rc = chrome.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);
                    });

我找到了这个链接,但我不明白该怎么做

https://bitbucket.org/chromiumembedded/cef/wiki/GeneralUsage.md#markdown-header-proxy-resolution 请写一些我喜欢的代码。

2 个答案:

答案 0 :(得分:3)

这次我真的回答了。

您需要做的是使用IRequestHandler实现您自己的类并调用GetAuthCredentials()。

public class MyRequestHandler : IRequestHandler
{

    bool IRequestHandler.GetAuthCredentials(IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
    {

        if (isProxy == true)
        {             
                callback.Continue("Username", "Password");

                return true;
        }

        return false;

     }
}

callback.Continue()将在调用时为您应用凭据。

然后,您可以在已有的代码中为您的浏览器实例实现处理程序。

Cef.UIThreadTaskFactory.StartNew(delegate
{

      chrome.RequestHandler = new MyRequestHandler();

      string ip = "IP";
      string port = "PORT";
      var rc = chrome.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);
});

答案 1 :(得分:0)

我使用的是cefsharp 65版,只需使用以下代码:

CefSharpSettings.Proxy = new ProxyOptions(ip: "myipaddress", port: "myport", username: "myusername", password: "mypassword");

将其放在Cef.Initialize()之前,对我有用