我在我的wpf应用程序中使用CefSharp Offscreen生成网站截图。它运行良好,但我需要检查页面上是否存在某些证书错误,并且应该在应用程序运行时配置它。
我添加了复选框:"忽略证书错误"来形成。然后我为ChrominiumWebBrowser IRequestHandler
实现了MyRequestHandler。
private class MyRequestHandler : IRequestHandler
{
public event EventHandler<LoadErrorEventArgs> CertificateError;
public bool IgnoreCertErrors { get; set; }
public bool OnBeforeBrowse(IWebBrowser browser, IRequest request, bool isRedirect, bool isMainFrame)
{
return false;
}
public bool OnCertificateError(IWebBrowser browser, CefErrorCode errorCode, string requestUrl)
{
if (IgnoreCertErrors)
{
return true;
}
var handler = CertificateError;
if (handler != null)
{
handler(browser, new LoadErrorEventArgs(requestUrl, errorCode, Resources.InvalidCert));
}
return false;
}
public void OnPluginCrashed(IWebBrowser browser, string pluginPath)
{
}
public CefReturnValue OnBeforeResourceLoad(IWebBrowser browser, IRequest request, bool isMainFrame)
{
return CefReturnValue.Continue;
}
public bool GetAuthCredentials(IWebBrowser browser, bool isProxy, string host, int port, string realm, string scheme, ref string username, ref string password)
{
return true;
}
public bool OnBeforePluginLoad(IWebBrowser browser, string url, string policyUrl, WebPluginInfo info)
{
return false;
}
public void OnRenderProcessTerminated(IWebBrowser browser, CefTerminationStatus status)
{
}
}
浏览器初始化:
browser = new ChromiumWebBrowser();
browser.LoadError += BrowserLoadError;
requestHandler = new MyRequestHandler() { IgnoreCertErrors = ignoreCertErrors };
requestHandler.CertificateError += BrowserLoadError;
browser.RequestHandler = requestHandler;
如果我使用IgnoreCertErrors = true
启动应用程序,则可以正常运行并忽略证书错误。
如果我从IgnoreCertErrors = false
开始,它也可以正常运行并引发CertificateError。
问题是我在应用程序运行时尝试更改IgnoreCertErrors
。当我用false启动app然后从false切换 - &gt;确实没关系,但是当我尝试将其从true更改为false时,OnCertificateError
方法没有被提升,因此我没有机会检查我的参数。
有缓存还是什么?也许整个我的方法都错了? 有没有可能重新初始化CefSharp?
更好的例子: XAML:
<Window x:Class="ceftest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Closed="Window_Closed">
<Grid>
<StackPanel>
<Button Grid.Column="1" Click="Button_Click">Generate Screenshot</Button>
<CheckBox Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked">Ignore cert errors</CheckBox>
</StackPanel>
</Grid>
</Window>
代码隐藏:
public partial class MainWindow : Window
{
ChromiumWebBrowser browser;
bool ignoreCertErrors = false;
string url = ""; // some page with invalid certificate
public MainWindow()
{
Cef.Initialize(new CefSettings());
InitializeComponent();
}
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
if (browser != null && browser.IsBrowserInitialized)
{
browser.Dispose();
}
browser = new ChromiumWebBrowser();
browser.LoadError += BrowserLoadError;
var requestHandler = new RequestHandler() { IgnoreCertErrors = ignoreCertErrors };
requestHandler.CertificateError += BrowserLoadError;
browser.RequestHandler = requestHandler;
System.Threading.Thread.Sleep(100);
browser.Size = new System.Drawing.Size(1000, 700);
browser.NewScreenshot += ScreenshotGenerated;
browser.Load(url);
}
private void ScreenshotGenerated(object sender, System.EventArgs e)
{
// I get here screenshot from browser.ScreenshotAsync and put it into Image control
}
private void BrowserLoadError(object sender, LoadErrorEventArgs e)
{
MessageBox.Show(e.ErrorCode + " " + e.ErrorText);
}
private void CheckBox_Checked(object sender, System.Windows.RoutedEventArgs e)
{
ignoreCertErrors = true;
}
private void CheckBox_Unchecked(object sender, System.Windows.RoutedEventArgs e)
{
ignoreCertErrors = false;
}
private void Window_Closed(object sender, EventArgs e)
{
Cef.Shutdown();
}
}
class RequestHandler : IRequestHandler
{
public event EventHandler<LoadErrorEventArgs> CertificateError;
public bool IgnoreCertErrors { get; set; }
public bool GetAuthCredentials(IWebBrowser browser, bool isProxy, string host, int port, string realm, string scheme, ref string username, ref string password)
{
return true;
}
public bool OnBeforeBrowse(IWebBrowser browser, IRequest request, bool isRedirect, bool isMainFrame)
{
return false;
}
public bool OnBeforePluginLoad(IWebBrowser browser, string url, string policyUrl, WebPluginInfo info)
{
return false;
}
public CefReturnValue OnBeforeResourceLoad(IWebBrowser browser, IRequest request, bool isMainFrame)
{
return CefReturnValue.Continue;
}
public bool OnCertificateError(IWebBrowser browser, CefErrorCode errorCode, string requestUrl)
{
if (IgnoreCertErrors)
{
return true;
}
var handler = CertificateError;
if (handler != null)
{
handler(browser, new LoadErrorEventArgs(requestUrl, errorCode, "certificate error"));
}
return false;
}
public void OnPluginCrashed(IWebBrowser browser, string pluginPath)
{
}
public void OnRenderProcessTerminated(IWebBrowser browser, CefTerminationStatus status)
{
}
}
CefSharp Offscreen版本41.0.1.0