添加ChromiumWebBrowser的RequestHandler处理类:
ResourceRequestHandler: 请求前处理类
class CustomResourceRequestHandler : CefSharp.Handler.ResourceRequestHandler { protected override CefReturnValue OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback) { //设置Referrer request.SetReferrer("http://xxx.xx/", ReferrerPolicy.Default); //可以根据不同Url请求,设置不同referer request.Url.StartsWith() if(request.Url.StartsWith("http://xxx.xx/")){ request.SetReferrer("http://xxx.xx/", ReferrerPolicy.Default); } } }
官方已指明不能在request.Headers里边直接处理:
RequestHandler: 请求处理类
class CustomRequestHandler : CefSharp.Handler.RequestHandler { protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling) { return new CustomResourceRequestHandler(); } }
添加RequestHandler处理:
chromiumWebBrowser1.RequestHandler = new CustomRequestHandler();