首页 > 解决方案 > 使用 IHTTPSecurity 绕过 Web 浏览器中的自签名证书

问题描述

我已经研究了IHTTPSECURITY使用 Delphi 实现接口的所有方法TWebBrowser。我正在尝试通过 https 导航到一个站点。该站点具有自签名证书。我希望避免在浏览器中弹出/出现安全警告。

如果我silent将浏览器的模式切换为 true,则不再弹出安全对话框,但浏览器会自动选择“否”作为“您要继续”问题的答案吗?

我相信解决这个问题的方法是通过 IHTTPSecurity 接口。我找到了为 EmbeddedWB ( why-doesnt-queryservice-get-call-for-ihttpsecurity-when-using-tembeddedwb ) 实现此功能的代码。我试图为 实现此功能TWebBrowser,但该onSecurityProblem功能不会触发。我的代码如下:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.OleCtrls, Vcl.StdCtrls, SHDocVw, ActiveX, Winapi.Urlmon, Winapi.WinInet;

  type TWebBrowser = class(SHDocVw.TWebBrowser, IHTTPSecurity, IWindowForBindingUI)
  private
    function GetWindow(const guidReason: TGUID; out hwnd): HRESULT; stdcall;
    function OnSecurityProblem(dwProblem: Cardinal): HRESULT; stdcall;
  end;


type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses System.NetEncoding;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
  var
    Flags, TargetFrameName, PostData, headers, url:OleVariant;
    authenticate:string;
begin
  webBrowser1.Navigate('about:blank');
  Flags := '1';
  TargetFrameName := '';
  PostData := '';

  Authenticate := TNetEncoding.Base64.Encode('MediaUser:test');
  Headers:='Authorization: Basic '+Authenticate;
  webBrowser1.Navigate(Edit1.text, flags, TargetFrameName, PostData, headers);
end;

function TWebBrowser.GetWindow(const guidReason: TGUID;
  out hwnd): HRESULT;
begin
  Result := S_FALSE;
end;


function TWebBrowser.OnSecurityProblem(dwProblem: Cardinal): HRESULT;
begin
  if (dwProblem = ERROR_INTERNET_INVALID_CA) or
     (dwProblem = ERROR_INTERNET_SEC_CERT_CN_INVALID)
    then Result := S_OK
    else Result := E_ABORT;
end;

end.

我究竟做错了什么?

作为一个附带问题,当上述应用程序终止时,我得到一个未指定的EoleException.

标签: delphitwebbrowser

解决方案


推荐阅读