无法实现 RequestHandler 以工作 CefSharp winforms C#
Can't achieve RequestHandler to work CefSharp winforms C#
我对 c# 和 CefSharp 有点陌生:-)所以对我保持冷静!
我的目标是调用一个 url,然后探索 DOM 以检索数据,然后处理这些数据以用于超监控目的。
我已经实现了检索,并且我的 js 代码使用 EvaluateScriptAsync 运行良好。
现在我想跟踪数据刷新。该页面由 websocket 通信更新,然后更新 DOM 对象模型中的值。
我想使用 RequestHandler 并适配 GetResourceResponseFilter;
但我无法做到这一点,而且我实施的方法似乎从未被调用过……
我使用从 cefsharp 项目站点下载的最小解决方案。我只是在这段代码的末尾添加了两行:
public BrowserForm()
{
InitializeComponent();
Text = "CefSharp";
WindowState = FormWindowState.Maximized;
browser = new ChromiumWebBrowser("www.google.com");
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;
我的测试处理程序看起来像这样!
public class MyBasicRequestHandler : CefSharp.Handler.RequestHandler
{
public bool OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
MessageBox.Show ("OnResourceResponse");
return false;
}
public bool OnProtocolExecution(IWebBrowser browserControl, IBrowser browser, string url)
{
MessageBox.Show("OnProtocolExecution : "+url);
return false;
}
public IResponseFilter GetResourceResponseFilter(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
MessageBox.Show("GetResourceResponseFilter : " + request.ReferrerUrl);
return null;
}
}
但是什么都没有被解雇!
如有任何帮助或建议,我们将不胜感激。
谢谢!
感谢 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 ;-)
我对 c# 和 CefSharp 有点陌生:-)所以对我保持冷静!
我的目标是调用一个 url,然后探索 DOM 以检索数据,然后处理这些数据以用于超监控目的。 我已经实现了检索,并且我的 js 代码使用 EvaluateScriptAsync 运行良好。
现在我想跟踪数据刷新。该页面由 websocket 通信更新,然后更新 DOM 对象模型中的值。
我想使用 RequestHandler 并适配 GetResourceResponseFilter; 但我无法做到这一点,而且我实施的方法似乎从未被调用过…… 我使用从 cefsharp 项目站点下载的最小解决方案。我只是在这段代码的末尾添加了两行:
public BrowserForm()
{
InitializeComponent();
Text = "CefSharp";
WindowState = FormWindowState.Maximized;
browser = new ChromiumWebBrowser("www.google.com");
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;
我的测试处理程序看起来像这样!
public class MyBasicRequestHandler : CefSharp.Handler.RequestHandler
{
public bool OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
MessageBox.Show ("OnResourceResponse");
return false;
}
public bool OnProtocolExecution(IWebBrowser browserControl, IBrowser browser, string url)
{
MessageBox.Show("OnProtocolExecution : "+url);
return false;
}
public IResponseFilter GetResourceResponseFilter(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
MessageBox.Show("GetResourceResponseFilter : " + request.ReferrerUrl);
return null;
}
}
但是什么都没有被解雇!
如有任何帮助或建议,我们将不胜感激。
谢谢!
感谢 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 ;-)