分享

【未解决】C#中WebBrowser中,如何(用程序模拟点击确定以)取消Security Alert的弹出窗口 | 在路上

 quasiceo 2015-04-02

【问题】

在C#模拟登陆12306网站的时候,遇到调用webbrowser打开地址:

https://dynamic.12306.cn/otsweb/order/querySingleAction.do?method=init

会出现安全警告的弹出窗口:

窗口1:

Revocation information for the security certificate for this site is not available. Do you want to proceed?

security alert

窗口2:

security alert - can not be verified

上述两次弹出窗口,都点击确定后,结果还会弹出另外一个窗口:

窗口3:

security warning

然后,才能正常显示界面:

 

normal page show

希望可以通过设置,不让这个security alert的窗口弹出,或通过程序代码模拟点击yes,以便不再麻烦用户点击。

【解决过程】

1.参考:Prevent Security alert in WebBrowser control,去设置了:

wbsBuyTicket.ScriptErrorsSuppressed = true;

结果是窗口1和2,都还是继续会弹出,只能避免掉窗口3的弹出。

所以只是解决了部分的问题。

2.参考:How to disable “Security Alert” window in Webbrowser control,添加了:

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

但是还是没用,上述窗口1和2,仍会弹出。

3.网上找了一堆,还是没人给出有效解决办法。不过也是有人想到了,通过写程序,找到弹出的窗口,然后用程序去模拟点击yes。此方法,貌似很复杂,还是能不用则不用吧。

4.参考了:Easy way to prevent WebBrowser control from using window.alert,去添加对应代码:

private void wbsBuyTicket_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
    if (wbsBuyTicket.Document != null)
    {

        // kills messagebox functionality by disabling the "window.alert()" function
        object window = wbsBuyTicket.Document.Window.DomWindow;
        if (window != null)
        {
            Type windowType = window.GetType();
            BindingFlags flags = BindingFlags.InvokeMethod | BindingFlags.Instance;
            string[] args = { "window.alert=function() {};", "JScript" };
            windowType.InvokeMember("[DispID=1165]", flags, null, window, args);
        }   // if
    }   // if
}

但是还是没用,因为在navigate之前,就弹出了窗口,所以此函数是在Navigated事件中处理的,所以没用。

也懒得再去试试其他事件,比如Navigating等,因为好像也是要根据程序代码去处理对应的特殊弹出窗口的代码的,此处没功夫去分析其中的代码。所以放弃此法。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多