Již jsem toto také řešil. Není to úplně jednoduché. Je potřeba použít COM rozhraní IHttpSecurity HTML Documentu. Důležité je, že při inicializaci události HttpSecurityEvent musí být již ve WebBrowseru načten nějaký dokument. To řeším v konstruktoru voláním Navigate na 'about:Blank'.
public Form1()
{
InitializeComponent();
//Important! You need to navigate first to initialize HttpSecurityEvent!
webBrowser1.Navigate("about:Blank");
}
#region WebBrowser COM interfaces
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("6d5140c1-7436-11ce-8034-00aa006009fa")]
internal interface IServiceProvider
{
[return: MarshalAs(UnmanagedType.IUnknown)]
object QueryService(ref Guid guidService, ref Guid riid);
}
[ComImport(), ComVisible(true)]
[Guid("cb728b20-f786-11ce-92ad-00aa00a74cd0")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IProfferService
{
void ProfferService(ref Guid guidService, IServiceProvider_ForProfferService psp, ref int cookie);
}
[ComImport, ComVisible(true)]
[Guid("6d5140c1-7436-11ce-8034-00aa006009fa")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IServiceProvider_ForProfferService
{
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int QueryService(
[In] ref Guid guidService,
[In] ref Guid riid,
[Out] out IntPtr ppvObject);
//This does not work i.e.-> ppvObject = (INewWindowManager)this
//[Out, MarshalAs(UnmanagedType.Interface)] out object ppvObject);
}
[ComImport(), ComVisible(true)]
[Guid("79eac9d7-bafa-11ce-8c82-00aa004ba90b")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IHttpSecurity
{
//IWindowForBindingUI
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int GetWindow(
[In] ref Guid rguidReason,
[In, Out] ref IntPtr phwnd);
//IHttpSecurity
//http://msdn.microsoft.com/library/default.asp?url=/workshop/networking/moniker/reference/ifaces/ihttpsecurity/onsecurityproblem.asp?frame=true
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int OnSecurityProblem(
[In, MarshalAs(UnmanagedType.U4)] uint dwProblem);
//dwProblem one of wininet error Messages
//http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wininet/wininet/wininet_errors.asp?frame=true
}
#endregion
#region HttpSecurityProblemHandler class
private class HttpSecurityProblemHandler : IServiceProvider_ForProfferService, IHttpSecurity
{
public bool IgnoreCertificateError { get; set; }
#region IServiceProvider_ForProfferService Members
public int QueryService(ref Guid guidService, ref Guid riid, out IntPtr ppvObject)
{
Guid IID_IHttpSecurity = typeof(IHttpSecurity).GUID;
if (this.IgnoreCertificateError && riid.Equals(IID_IHttpSecurity))
{
ppvObject = Marshal.GetComInterfaceForObject(this, typeof(IHttpSecurity));
return 0 /*S_OK*/;
}
ppvObject = IntPtr.Zero;
return unchecked((int)0x800C0011);
}
#endregion
#region IHttpSecurity Members
public int GetWindow(ref Guid rguidReason, ref IntPtr phwnd)
{
Guid IID_IHttpSecurity = typeof(IHttpSecurity).GUID;
phwnd = IntPtr.Zero;
if (rguidReason == IID_IHttpSecurity)
{
return 0; //S_OK
}
return 1; //S_FALSE
}
public int OnSecurityProblem(uint dwProblem)
{
return 0; //S_OK - Ignore certificate error
}
#endregion
}
#endregion
private HttpSecurityProblemHandler SecurityProblemHandler;
private void Form1_Load(object sender, EventArgs e)
{
this.SecurityProblemHandler = new HttpSecurityProblemHandler();
SecurityProblemHandler.IgnoreCertificateError = true; //Ignore All Certificate Errors
InitHttpSecurityEvent();
}
private void InitHttpSecurityEvent()
{
//You need to navigate first to initialize this
IServiceProvider serviceProvider = (IServiceProvider)webBrowser1.Document.DomDocument; //DomDocument is IHTMLDocument2;
Guid SID_SProfferService = new Guid("cb728b20-f786-11ce-92ad-00aa00a74cd0");
//Get a reference to the IProfferService interface
Guid serviceGuid = SID_SProfferService;
Guid iid = typeof(IProfferService).GUID;
var profferService = (IProfferService)serviceProvider.QueryService(ref serviceGuid, ref iid);
//Based on http://www.dotnet247.com/247reference/msgs/62/311293.aspx
//Register calls from HttpSecurity interface
Guid IID_IHttpSecurity = typeof(IHttpSecurity).GUID;
int cookieIHttpSecurity = 0;
profferService.ProfferService(IID_IHttpSecurity, SecurityProblemHandler, ref cookieIHttpSecurity);
}
Dejte vědět zda vám to funguje.
|