于需要重定向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的事件中】-
1234567891011121314
publicstaticvoidSetSilent(WebBrowser browser,boolsilent){FieldInfo fiComWebBrowser =typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);if(fiComWebBrowser ==null)return;objectobjComWebBrowser = fiComWebBrowser.GetValue(browser);if(objComWebBrowser ==null)return;objComWebBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty,null, objComWebBrowser,newobject[] { silent });}SetSilent(senderasWebBrowser,true); 方法二:【 SetSilent方法调用据说要放在webbrowser的Navigated的事件中】-
123456789101112131415161718192021222324252627282930313233
publicstaticvoidSetSilent(WebBrowser browser,boolsilent){if(browser ==null)thrownewArgumentNullException("browser");// get an IWebBrowser2 from the documentIOleServiceProvider sp = browser.DocumentasIOleServiceProvider;if(sp !=null){Guid IID_IWebBrowserApp =newGuid("0002DF05-0000-0000-C000-000000000046");Guid IID_IWebBrowser2 =newGuid("D30C1661-CDAF-11d0-8A3E-00C04FC9E26E");objectwebBrowser;sp.QueryService(refIID_IWebBrowserApp,refIID_IWebBrowser2,outwebBrowser);if(webBrowser !=null){webBrowser.GetType().InvokeMember("Silent", BindingFlags.Instance | BindingFlags.Public | BindingFlags.PutDispProperty,null, webBrowser,newobject[] { silent });}}}[ComImport, Guid("6D5140C1-7436-11CE-8034-00AA006009FA"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]privateinterfaceIOleServiceProvider{[PreserveSig]intQueryService([In]refGuid guidService, [In]refGuid riid, [MarshalAs(UnmanagedType.IDispatch)]outobjectppvObject);}SetSilent(senderasWebBrowser,true); 方法三:-
123456789
WebBrowser t = senderasWebBrowser;dynamic activeX = t.GetType().InvokeMember("ActiveXInstance",BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,null,t,newobject[] { });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);
|