WebBrowser control - page rendering error after in

2019-02-21 06:53发布

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.

1条回答
ゆ 、 Hurt°
2楼-- · 2019-02-21 07:37

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+\()(Windows\s+.*)like Gecko\s*$", 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.");
    }
}
查看更多
登录 后发表回答