WebBrowser 控件 - 安装 IE 11 后页面呈现错误 [英] WebBrowser control - page rendering error after install IE 11

查看:32
本文介绍了WebBrowser 控件 - 安装 IE 11 后页面呈现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在安装 Internet Explorer 11 Preview 后遇到 Winforms .NET 类WebBrowser"问题.当我调用我的网页时,它看起来像是禁用了 javascipt.

解决方案

如果您的基于 WebBrowser 的应用程序和您的网页在 IE10 上仍然可以正常工作,以下原因可能是问题.

在一个颇有争议的决定中,微软改变了IE11中IE用户代理(UA)字符串的传统布局.

这是 IE11 中 UI 字符串的样子:

<前>navigator.userAgent: Mozilla/5.0 (Windows NT 6.2; WOW64; Trident/7.0; rv:11.0) 像 Gecko文档.文档模式:11document.compatMode: CSS1Compat

这是它在 IE10 和旧版本中的样子:

<前>navigator.userAgent:Mozilla/5.0(兼容;MSIE 10.0;Windows NT 6.2;WOW64;Trident/6.0;)文档.文档模式:10document.compatMode: CSS1Compat

虽然一个设计良好的网页不应该依赖于 UA 字符串来检测可用的 HTML 功能,但许多现有的页面仍然可以,而且这种变化可能会让他们感到困惑.

如果您无法控制加载的网页,并且无法修复它们,恢复传统 UA 字符串的一种方法是使用 FEATURE_BROWSER_EMULATION 用于基于 WebBrowser 的应用.不幸的是,您必须低至 IE7.指定更高版本不会恢复旧的 UA 字符串布局.

另一种更灵活但更复杂的解决方法是通过 UrlMkSetSessionOption/URLMON_OPTION_USERAGENT WinAPI 设置自定义 UA 字符串.您应该使用 UrlMkGetSessionOption 检索当前 UI 字符串,解析它,添加缺少的部分并使用 UrlMkSetSessionOption 将其设置回来.在 WebBrowser 对象被实例化之前,在 Form 类的 static 构造函数中执行此操作.

[UPDATE] 更改用户代理字符串的代码:

static public void ChangeUserAgentForIE11(){如果 (GetIEVersion() <= 10)返回;var userAgent = new StringBuilder(256);整数长度 = 0;Win32.UrlMkGetSessionOption(Win32.URLMON_OPTION_USERAGENT, userAgent, userAgent.Capacity-1, ref length, 0);//IE10:navigator.userAgent:Mozilla/5.0(兼容;MSIE 10.0;Windows NT 6.2;WOW64;Trident/6.0;.NET4.0E;.NET4.0C;.NET CLR 3.5.30729;.NET CLR 2.0.5077;.NET CLR 3.0.30729;媒体中心 PC 6.0;平板电脑 2.0)//IE11: navigator.userAgent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; Media Center PC 6.0; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; rv:11.0) 像 Geckovar regex = new Regex(@"^s*(Mozilla/d+.d+s+()(Windowss+.*)like Geckos*$", RegexOptions.IgnoreCase);//IE11 regexvar match = regex.Match(userAgent.ToString());如果(匹配.成功){var newUserAgent = String.Concat(match.Groups[1], "兼容; MSIE 10.0; ", match.Groups[2]);Win32.UrlMkSetSessionOption(Win32.URLMON_OPTION_USERAGENT, newUserAgent, newUserAgent.Length, 0);var verifyUserAgent = new StringBuilder(256);长度 = 0;Win32.UrlMkGetSessionOption(Win32.URLMON_OPTION_USERAGENT, verifyUserAgent, verifyUserAgent.Capacity-1, ref length, 0);if (verifyUserAgent.ToString() != newUserAgent)throw new ApplicationException("无法更改 WebBrowser 用户代理.");}}

I have problem with Winforms .NET Class "WebBrowser" after installing of Internet Explorer 11 Preview. It looks like disable javascipt when I call my web page.

解决方案

If your WebBrowser-based app and your web pages still work fine with IE10, the following is likely to be the reason for the problem.

In a rather controversial decision, Microsoft has changed the traditional layout of IE User Agent (UA) string in IE11.

This is what the UI string looks like in IE11:

 navigator.userAgent: Mozilla/5.0 (Windows NT 6.2; WOW64; Trident/7.0; rv:11.0) like Gecko
 document.documentMode: 11
 document.compatMode: CSS1Compat

This is what it used to look like in IE10 and older versions:

 navigator.userAgent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0;)
 document.documentMode: 10
 document.compatMode: CSS1Compat

While a well-designed web page should not rely upon UA string to detect available HTML features, a lot of existing pages still do, and such change may have confused them.

If you have no control over the web pages you load, and cannot fix them, one way to restore the traditional UA string is to force IE7 emulation with FEATURE_BROWSER_EMULATION for your WebBrowser-based app. Unfortunately, you'd have to go for as low as IE7. Specifying a higher version does't restore the old UA string layout.

Another, more flexible but more complex way to fix this is to set up a custom UA string via UrlMkSetSessionOption/URLMON_OPTION_USERAGENT WinAPI. You should retrieve the current UI string with UrlMkGetSessionOption, parse it, add the missing parts and set it back with UrlMkSetSessionOption. Do it in the static constructor of your Form class, before WebBrowser object gets instantiated.

[UPDATE] The code for changing the user agent string:

static public void ChangeUserAgentForIE11()
{
    if (GetIEVersion() <= 10)
        return;

    var userAgent = new StringBuilder(256);
    int length = 0;
    Win32.UrlMkGetSessionOption(Win32.URLMON_OPTION_USERAGENT, userAgent, userAgent.Capacity-1, ref length, 0);

    // IE10: navigator.userAgent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0)
    // IE11: navigator.userAgent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; Media Center PC 6.0; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; rv:11.0) like Gecko

    var regex = new Regex(@"^s*(Mozilla/d+.d+s+()(Windowss+.*)like Geckos*$", RegexOptions.IgnoreCase); //IE11 regex
    var match = regex.Match(userAgent.ToString());
    if (match.Success)
    {
        var newUserAgent = String.Concat(match.Groups[1], "compatible; MSIE 10.0; ", match.Groups[2]);
        Win32.UrlMkSetSessionOption(Win32.URLMON_OPTION_USERAGENT, newUserAgent, newUserAgent.Length, 0);
        var verifyUserAgent = new StringBuilder(256);
        length = 0;
        Win32.UrlMkGetSessionOption(Win32.URLMON_OPTION_USERAGENT, verifyUserAgent, verifyUserAgent.Capacity-1, ref length, 0);
        if (verifyUserAgent.ToString() != newUserAgent)
            throw new ApplicationException("Unable to change WebBrowser User Agent.");
    }
}

这篇关于WebBrowser 控件 - 安装 IE 11 后页面呈现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆