WebBrowser Control opens HTTPS site

Source: Internet
Author: User

Background:

As with the previous blog post, this article is also derived from the problems encountered in the development dropboxapi. Because you need to redirect the HTTPS type Web site, but your own HTTPS certificate is self-signed, always prompt ' Web site security certificate has problems '.

This, check a little information, however, many methods for I do not have any eggs, but later still usable, so finishing "Of course there are some effective methods"

First, you're using WebBrowser in WPF.

1, set ServicePointManager validation callback, however, this method can only be used in managed code such as HttpRequest, WebBrowser implementation does not apply. The code is as follows:

Servicepointmanager.servercertificatevalidationcallback = new Remotecertificatevalidationcallback ( validateservercertificate);p ublic static bool Validateservercertificate (object sender, X509Certificate certificate, X509chain chain, Sslpolicyerrors sslpolicyerrors) {return true;}

2, in the case of 1 invalid, and some friends said to set upScriptErrorsSuppressed属性为true,然而wpf中的webbrowser并没有此属性,故有以下方法添加相似设置。【注意:此方法应该是正对脚本执行的】

  • 方法一:【 SetSilent方法调用据说要放在webbrowser的Navigated的事件中】
  • 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的事件中】
  •         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 GUIDs Guidservice, [in] ref GUIDs riid, [MarshalAs (Unmanagedtype.idispatch)] out object        Ppvobject); }setsilent (sender as WebBrowser, true);
  • 方法三:
  •                 WebBrowser t = sender as WebBrowser;                                Dynamic ActiveX = T.gettype (). InvokeMember (                    "Activexinstance",                    BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,                    null,                    T,                    new object[] {});                Activex.silent = true;

However, WPF is set up like nothing, instead of using WinForm to try, directly can set the scripterrorssuppressed, but the problem comes, set to true, do not play the window, but I want to point is the window ' is (Y) ' Ah, set directly after the equivalent of the election ' No (N) ' out.

However, some people on the internet said to use Axwebbrowser, I also added a COM to try, the difference is that the silent property is set, the effect and WinForm basically the same ...

"Note:" WPF and WinForm in the WebBrowser also a bit different, WPF with the version of IE is higher, the HTTPS certificate is not displayed directly in the page, and the version of IE in WinForm should be lower, the HTTPS certificate is not right when the window popup.

Here's how it works:

1, with WinForm in the WebBrowser, automatically click ' Yes ', using the window API

        [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, "Security Alert");            SetForegroundWindow (ihandle);            System.Windows.Forms.SendKeys.SendWait ("Y%");

2, the last one is to solve their own problems encountered the method:

Use WebBrowser in WPF to determine the current URI in the WebBrowser navigating event, automatically swapping to a non-HTTP URI

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

If you have other solutions, please contact us.

Related issues Address:

How to disable the WebBrowser control in the Security Alerts window

Is it possible to use ServicePointManager with Webbrowser control?

WPF WebBrowser control-how to supress script errors?

How to disable "Security Warning" windows in Webbrowser control

SSL Support-supports certificates in your application using the. NET Framework 2.0

WebBrowser Control opens HTTPS site

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.