感谢 amaitland 的友好评论,此代码有效。我只向从 github 提取的 CefSharp.Minimal Example.Winforms 解决方案添加了几行来注册 RequestHandler。
public BrowserForm()
{
InitializeComponent();
Text = "CefSharp";
WindowState = FormWindowState.Maximized;
browser = new ChromiumWebBrowser("https://cefsharp.github.io/");
toolStripContainer.ContentPanel.Controls.Add(browser);
browser.IsBrowserInitializedChanged += OnIsBrowserInitializedChanged;
browser.LoadingStateChanged += OnLoadingStateChanged;
browser.ConsoleMessage += OnBrowserConsoleMessage;
browser.StatusMessage += OnBrowserStatusMessage;
browser.TitleChanged += OnBrowserTitleChanged;
browser.AddressChanged += OnBrowserAddressChanged;
// Add this for request handler
Handler.RequestHandler requestHandler = new MyBasicRequestHandler();
browser.RequestHandler = requestHandler;
var version = string.Format("Chromium: {0}, CEF: {1}, CefSharp: {2}",
Cef.ChromiumVersion, Cef.CefVersion, Cef.CefSharpVersion);
var bitness = Environment.Is64BitProcess ? "x64" : "x86";
var environment = String.Format("Environment: {0}", bitness);
DisplayOutput(string.Format("{0}, {1}", version, environment));
}
然后我为 MyBasicRequestHandler 和 MyCustomResourceRequestHandler 类添加代码如下:
public class MyCustomResourceRequestHandler : CefSharp.Handler.ResourceRequestHandler
{
private readonly System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
protected override IResponseFilter GetResourceResponseFilter(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
return new CefSharp.ResponseFilter.StreamResponseFilter(memoryStream);
}
protected override void OnResourceLoadComplete(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
{
//You can now get the data from the stream
var bytes = memoryStream.ToArray();
if (response.Charset == "utf-8")
{
var str = System.Text.Encoding.UTF8.GetString(bytes);
Console.WriteLine("In OnResourceLoadComplete : " + str.Substring(0,10) + " <...>");
}
else
{
//Deal with different encoding here
}
}
}
public class MyBasicRequestHandler : CefSharp.Handler.RequestHandler
{
protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
{
Console.WriteLine("In GetResourceRequestHandler : " + request.Url);
//Only intercept specific Url's
if (request.Url == "http://cefsharp.github.io/" || request.Url == "https://cefsharp.github.io/")
{
return new MyCustomResourceRequestHandler();
}
//Default behaviour, url will be loaded normally.
return null;
}
}
启动应用程序并很高兴在 VisualStudio 控制台输出中看到这一点:
In GetResourceRequestHandler : https://cefsharp.github.io/
In OnResourceLoadComplete : <!DOCTYPE <...>
In GetResourceRequestHandler : https://fonts.googleapis.com/css?family=Ubuntu:400,500
In GetResourceRequestHandler : https://cefsharp.github.io/stylesheets/stylesheet.css
In GetResourceRequestHandler : https://cefsharp.github.io/stylesheets/github-light.css
In GetResourceRequestHandler : https://sidecar.gitter.im/dist/sidecar.v1.js
In GetResourceRequestHandler : https://cefsharp.github.io/images/logo2.png
In GetResourceRequestHandler : https://cefsharp.github.io/images/diagram1.png
In GetResourceRequestHandler : https://cefsharp.github.io/images/diagram2.png
In GetResourceRequestHandler : https://cefsharp.github.io/images/diagram3.png
In GetResourceRequestHandler : https://cefsharp.github.io/images/diagram4.png
In GetResourceRequestHandler : https://cefsharp.github.io/images/diagram5.png
In GetResourceRequestHandler : https://cefsharp.github.io/images/diagram7.png
In GetResourceRequestHandler : https://cefsharp.github.io/images/diagram6.png
In GetResourceRequestHandler : https://cefsharp.github.io/stylesheets/print.css
In GetResourceRequestHandler : https://cefsharp.github.io/images/top-header.png
In GetResourceRequestHandler : https://fonts.gstatic.com/s/ubuntu/v14/4iCs6KVjbNBYlgoKfw72.woff2
In GetResourceRequestHandler : https://fonts.gstatic.com/s/ubuntu/v14/4iCv6KVjbNBYlgoCjC3jsGyN.woff2
所以我的问题解决了!再次感谢亲爱的amaitland ;-)