WebBrowser控件打开https站点

于需要重定向https类型网站,但自己的https证书是自签名的,总是提示‘网站的安全证书存在问题’。

鉴此,查了些许资料,然而许多方法对我并没有什么卵用,不过以后还是可用用上的,故整理下【当然其中也有一些有效的方法】

首先,自己使用的是WPF中的WebBrowser

1、设置ServicePointmanager的验证回调,然而,这方法只能用于httprequest等托管代码中,webbrowser实现并不适用。代码如下:

1
2
3
4
5
6
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
 
public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    return true;
}

2、在1无效的情况下,又有些朋友说要设置ScriptErrorsSuppressed属性为true,然而wpf中的webbrowser并没有此属性,故有以下方法添加相似设置。【注意:此方法应该是正对脚本执行的】

  • 方法一:【 SetSilent方法调用据说要放在webbrowser的Navigated的事件中】
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    public static void SetSilent(WebBrowser browser, bool silent)
           {
               FieldInfo fiComWebBrowser = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
               if (fiComWebBrowser == null)
                   return;
               object objComWebBrowser = fiComWebBrowser.GetValue(browser);
               if (objComWebBrowser == null)
                   return;
               objComWebBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, objComWebBrowser, new object[] { silent });
           }
     
     
     
    SetSilent(sender as WebBrowser, true);
  • 方法二:【 SetSilent方法调用据说要放在webbrowser的Navigated的事件中】
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
            public static void SetSilent(WebBrowser browser, bool silent)
            {
                if (browser == null)
                    throw new ArgumentNullException("browser");
     
                // get an IWebBrowser2 from the document
                IOleServiceProvider sp = browser.Document as IOleServiceProvider;
                if (sp != null)
                {
                    Guid IID_IWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
                    Guid IID_IWebBrowser2 = new Guid("D30C1661-CDAF-11d0-8A3E-00C04FC9E26E");
     
                    object webBrowser;
                    sp.QueryService(ref IID_IWebBrowserApp, ref IID_IWebBrowser2, out webBrowser);
                    if (webBrowser != null)
                    {
                        webBrowser.GetType().InvokeMember("Silent", BindingFlags.Instance | BindingFlags.Public | BindingFlags.PutDispProperty, null, webBrowser, new object[] { silent });
                    }
                }
            }
     
     
            [ComImport, Guid("6D5140C1-7436-11CE-8034-00AA006009FA"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
            private interface IOleServiceProvider
            {
                [PreserveSig]
                int QueryService([In] ref Guid guidService, [In] ref Guid riid, [MarshalAs(UnmanagedType.IDispatch)] out object ppvObject);
            }
     
     
     
     
    SetSilent(sender as WebBrowser, true);
  • 方法三:
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    WebBrowser t = sender as WebBrowser;
     
    dynamic activeX = t.GetType().InvokeMember(
        "ActiveXInstance",
        BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
        null,
        t,
        new object[] { });
    activeX.Silent = true;

 

然而wpf设置了好像没啥用,转而使用winform试试,直接就可以设置ScriptErrorsSuppressed,不过问题来了,设置为true之后,不弹窗了,但我想点的是弹窗的‘是(Y)’啊,设置之后直接就相当于选‘否(N)’了。

不过,网上还有人说要用AxWebBrowser,我也就加了个com试试,不同的是其中设置的是Silent属性,效果和winform基本一样。。。

【注:】wpf与winform中的webbrowser还有一点不一样,wpf用的ie版本好像比较高,https证书不对时直接在页面内显示,而winform中ie版本应该比较低,https证书不对时直接弹窗。

下面就是有效的方法啦:

1、用winform中的webbrowser,自动点击‘是’【使用window api】

1
2
3
4
5
6
7
8
9
10
[DllImport("user32.dll")]
public extern static int FindWindow(string lpclassname, string lpwindowname);
 
[DllImport("user32.dll")]
public extern static void SetForegroundWindow(int handle);
 
 
    int iHandle = FindWindow(null, "安全警报");
    SetForegroundWindow(iHandle);
    System.Windows.Forms.SendKeys.SendWait("Y%");

2、最后一种是自己解决遇到的问题的方法:

使用wpf中的webbrowser,在webbrowser的Navigating事件中,判断当前的uri,自动换为非http的uri

1
(sender as WebBrowser).Navigate(http://www.xxx.com);

 

posted @ 2020-03-19 14:39  左正  阅读(2133)  评论(0编辑  收藏  举报