最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

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

C# crifan 5541浏览 0评论

【问题】

在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等,因为好像也是要根据程序代码去处理对应的特殊弹出窗口的代码的,此处没功夫去分析其中的代码。所以放弃此法。

5.看到这里:

C# WebBrowser 屏蔽alert,confirm的方法 – 日里仙人 – 博客园

但是看不太懂,也觉得很复杂,懒得弄了。

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

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (2)

  1. 没有。不过类似的方法和技术,基本算是知道。因为之前用python折腾过博客搬家到wordpress,所以关于模拟登陆这部分的内容,算是有些了解的。
    crifan12年前 (2012-05-08)回复
  2. 你好,请问你研究过c# 模拟http方式登录QQ空间吗
    落叶12年前 (2012-05-08)回复
85 queries in 0.166 seconds, using 22.16MB memory