gpt4 book ai didi

c#-4.0 - 如何绕过 CefSharp WinForms SSL 错误

转载 作者:行者123 更新时间:2023-12-01 17:17:08 25 4
gpt4 key购买 nike

我正在使用 CefSharp.WinForms 开发应用程序。当发生任何 SSL 证书错误时,将不会显示网页。

如何绕过 SSL 证书错误并显示网页?

最佳答案

选项 1(首选)

实现IRequestHandler.OnCertificateError - 对于每个无效证书都会调用此方法。如果您只想重写 IRequestHandler 的一些方法,那么您可以继承 RequestHandler覆盖您特别感兴趣的方法,在本例中为OnCertificateError

//Make sure you assign your RequestHandler instance to the `ChromiumWebBrowser`
browser.RequestHandler = new ExampleRequestHandler();

public class ExampleRequestHandler : RequestHandler
{
protected override bool OnCertificateError(IWebBrowser chromiumWebBrowser, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback)
{
//NOTE: We also suggest you wrap callback in a using statement or explicitly execute callback.Dispose as callback wraps an unmanaged resource.

//Example #1
//Return true and call IRequestCallback.Continue() at a later time to continue or cancel the request.
//In this instance we'll use a Task, typically you'd invoke a call to the UI Thread and display a Dialog to the user
Task.Run(() =>
{
//NOTE: When executing the callback in an async fashion need to check to see if it's disposed
if (!callback.IsDisposed)
{
using (callback)
{
//We'll allow the expired certificate from badssl.com
if (requestUrl.ToLower().Contains("https://expired.badssl.com/"))
{
callback.Continue(true);
}
else
{
callback.Continue(false);
}
}
}
});

return true;

//Example #2
//Execute the callback and return true to immediately allow the invalid certificate
//callback.Continue(true); //Callback will Dispose it's self once exeucted
//return true;

//Example #3
//Return false for the default behaviour (cancel request immediately)
//callback.Dispose(); //Dispose of callback
//return false;
}
}
<小时/>

选项 2

设置忽略证书错误命令行参数

var settings = new CefSettings();
settings.CefCommandLineArgs.Add("ignore-certificate-errors");

Cef.Initialize(settings);

关于c#-4.0 - 如何绕过 CefSharp WinForms SSL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35555754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com