CefSharp访问需要认证网页或接口(在Request的Headers中添加认证Token)

……
private ChromiumWebBrowser browser;
……
browser.RequestHandler = new MyRequestHandler();
……
 public class MyRequestHandler : DefaultRequestHandler
    {
        public static readonly string VersionNumberString = String.Format("Chromium: {0}, CEF: {1}, CefSharp: {2}",
            Cef.ChromiumVersion, Cef.CefVersion, Cef.CefSharpVersion);

        public override CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
        {
            Uri url;
            if (Uri.TryCreate(request.Url, UriKind.Absolute, out url) == false)
            {
                return CefReturnValue.Cancel;
            }
            var headers = request.Headers; 
            headers["Authorization"] =Program.Token; //传递进去认证Token
            request.Headers = headers; 
            return CefReturnValue.Continue;
        }       
    }

 

你可能感兴趣的:(C#)