C# CefSharp Filter RequestHandler Pictures
1. Method 1
ChromiumWebBrowser Implements IRequestHandler
For details, refer to the Appendix; replacing the OnBeforeResourceLoad method with the content in 2 is very simple;
2. Method 2
Inherit the default abstract class DefaultRequestHandler
internal class RequestHandler : DefaultRequestHandler { public override CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback) { if ( == ) return ; return ; } }
Comparing the implementation of interface methods and inheriting the default abstract classes, it is found that abstract classes can only implement related processing methods and are more flexible. If IRequestHandler is used, all methods in the RequestHandler interface need to be implemented, otherwise an unimplemented exception will be thrown;
Unlike other methods of filtering resource images from the Response link online, the methods mentioned here can save traffic, speed up page access, etc. before requesting image resources.
appendix:
using System; using ; using .X509Certificates; using ; using ; using ; namespace { /// <summary> /// DefaultRequestHandler provides a base class for you to inherit from /// you only need to implement the methods that are relevant to you. /// If you implement the IRequestHandler interface you will need to /// implement every method /// </summary> public class RequestHandler : IRequsetHandler { public static readonly string VersionNumberString = ("Chromium: {0}, CEF: {1}, CefSharp: {2}", , , ); private Dictionary<UInt64, MemoryStreamResponseFilter> responseDictionary = new Dictionary<UInt64, MemoryStreamResponseFilter>(); public override bool OnBeforeBrowse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, bool userGesture, bool isRedirect) { return false; } public override bool OnOpenUrlFromTab(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture) { return false; } public override bool OnCertificateError(IWebBrowser browserControl, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback) { //NOTE: If you do not wish to implement this method returning false is the default behaviour // We also suggest you explicitly Dispose of the callback as it wraps an unmanaged resource. //(); //return false; //NOTE: When executing the callback in an async fashion need to check to see if it's disposed if (!) { using (callback) { //To allow certificate //(true); //return true; } } return false; } public override void OnPluginCrashed(IWebBrowser browserControl, IBrowser browser, string pluginPath) { // TODO: Add your own code here for handling scenarios where a plugin crashed, for one reason or another. } public override CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback) { Uri url; if ((, , out url) == false) { //If we're unable to parse the Uri then cancel the request // avoid throwing any exceptions here as we're being called by unmanaged code return ; } //Example of how to set Referer // Same should work when setting any header // For this example only set Referer when using our custom scheme if ( == ) { //Referrer is now set using it's own method (was previously set in headers before) ("", ); } //Example of setting User-Agent in every request. //var headers = ; //var userAgent = headers["User-Agent"]; //headers["User-Agent"] = userAgent + " CefSharp"; // = headers; //NOTE: If you do not wish to implement this method returning false is the default behaviour // We also suggest you explicitly Dispose of the callback as it wraps an unmanaged resource. //(); //return false; //NOTE: When executing the callback in an async fashion need to check to see if it's disposed if (!) { using (callback) { if ( == "POST") { using (var postData = ) { if(postData != null) { var elements = ; var charSet = (); foreach (var element in elements) { if ( == ) { var body = (charSet); } } } } } //Note to Redirect simply set the request Url //if (("", )) //{ // = "/"; //} //Callback in async fashion //(true); //return ; } } return ; } public override bool GetAuthCredentials(IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback) { //NOTE: If you do not wish to implement this method returning false is the default behaviour // We also suggest you explicitly Dispose of the callback as it wraps an unmanaged resource. (); return false; } public override bool OnSelectClientCertificate(IWebBrowser browserControl, IBrowser browser, bool isProxy, string host, int port, X509Certificate2Collection certificates, ISelectClientCertificateCallback callback) { //NOTE: If you do not wish to implement this method returning false is the default behaviour // We also suggest you explicitly Dispose of the callback as it wraps an unmanaged resource. (); return false; } public override void OnRenderProcessTerminated(IWebBrowser browserControl, IBrowser browser, CefTerminationStatus status) { // TODO: Add your own code here for handling scenarios where the Render Process terminated for one reason or another. (); } public override bool OnQuotaRequest(IWebBrowser browserControl, IBrowser browser, string originUrl, long newSize, IRequestCallback callback) { //NOTE: If you do not wish to implement this method returning false is the default behaviour // We also suggest you explicitly Dispose of the callback as it wraps an unmanaged resource. //(); //return false; //NOTE: When executing the callback in an async fashion need to check to see if it's disposed if (!) { using (callback) { //Accept Request to raise Quota //(true); //return true; } } return false; } public override void OnResourceRedirect(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, ref string newUrl) { //Example of how to redirect - need to check `newUrl` in the second pass //if (("", ) && !("github")) //{ // newUrl = ""; //} } public override bool OnProtocolExecution(IWebBrowser browserControl, IBrowser browser, string url) { return ("mailto"); } public override void OnRenderViewReady(IWebBrowser browserControl, IBrowser browser) { } public override bool OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response) { //NOTE: You cannot modify the response, only the request // You can now access the headers //var headers = ; return false; } public override IResponseFilter GetResourceResponseFilter(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response) { var url = new Uri(); if ( == ) { if((, )) { return new FindReplaceResponseFilter("REPLACE_THIS_STRING", "This is the replaced string!"); } if (("custom://cefsharp/assets/js/", )) { return new AppendResponseFilter( + "//CefSharp Appended this comment."); } //Only called for our customScheme var dataFilter = new MemoryStreamResponseFilter(); (, dataFilter); return dataFilter; } //return new PassThruResponseFilter(); return null; } public override void OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength) { var url = new Uri(); if ( == ) { MemoryStreamResponseFilter filter; if((, out filter)) { //TODO: Do something with the data here var data = ; var dataLength = ; //NOTE: You may need to use a different encoding depending on the request var dataAsUtf8String = Encoding.(data); } } } } }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.