网页在 IE、Chrome 和 Firefox 中有效,但在使用 .NET WebBrowser 控件时无效 [英] Webpage works in IE, Chrome and Firefox, but not when using the .NET WebBrowser control

查看:42
本文介绍了网页在 IE、Chrome 和 Firefox 中有效,但在使用 .NET WebBrowser 控件时无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Visual Studio 2010 中使用 WebBrowser 控件并尝试显示页面:

<小时>

更新:

使用 Visual Vincent 的回答可以很好地加载页面.

但是网站上的flash视频(或者我认为它类似于flash)无法播放.请参阅下图中的比较.

奇怪的是,如果我打开 YouTube,Flash 效果很好.经过一番研究,它似乎是由其他原因引起的.任何线索如何解决它?

Internet Explorer - 工作正常:

WebBrowser 控件 - 由于某种原因视频卡住无法播放:

解决方案

可能与 WebBrowser 控件默认使用 IE 7 的文档模拟模式有关,这意味着所有页面都是使用 Internet Explorer 7 引擎处理.由于该版本很旧,今天的大多数网站都不兼容它,这会影响您访问页面时的功能.

您可以通过在 HKEY_LOCAL_MACHINE 配置单元中的注册表项 Software\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION 中为您的应用程序添加一个值来更改此行为或 HKEY_CURRENT_USER.通过这样做,您将强制您的应用程序使用特定版本的 IE 引擎.

我编写了一个类来帮助您解决这个问题:

'用于更改 WebBrowser 控件的文档模拟的类.'由视觉文森特撰写,2017 年.导入 Microsoft.Win32导入 System.Security导入 System.Windows.FormsPublic NotInheritable 类 InternetExplorer私有子新建()结束子Public Const InternetExplorerRootKey As String = "Software\Microsoft\Internet Explorer"Public Const BrowserEmulationKey As String = InternetExplorerRootKey &"\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION"Public Const ActiveXObjectCachingKey As String = InternetExplorerRootKey &"\MAIN\FeatureControl\FEATURE_OBJECT_CACHING"Private Shared ReadOnly WebBrowserInstance As New WebBrowser '用于以 .NET 友好的方式获取当前的 IE 版本.公共枚举 BrowserEmulation 作为整数IE7 = 7000IE8 = 8000IE8 标准 = 8888IE9 = 9000IE9 标准 = 9999IE10 = 10000IE10 标准 = 10001IE11 = 11000IE11Edge = 11001结束枚举Public Shared Sub SetLatestBrowserEmulation(ByVal Root As RegistryRoot)Dim Emulation As BrowserEmulation = BrowserEmulation.IE7选择案例 WebBrowserInstance.Version.MajorCase Is >= 11 : Emulation = BrowserEmulation.IE11Edge案例 10:仿真 = BrowserEmulation.IE10Standards案例 9:仿真 = BrowserEmulation.IE9Standards案例 8:仿真 = BrowserEmulation.IE8Standards结束选择InternetExplorer.SetBrowserEmulation(Root, Emulation)结束子Public Shared Sub SetBrowserEmulation(ByVal Root As RegistryRoot,ByVal Emulation As BrowserEmulation)使用 RootKey 作为 RegistryKey = Root.RootDim EmulationKey As RegistryKey = RootKey.OpenSubKey(BrowserEmulationKey, True)如果 EmulationKey 是什么,那么 EmulationKey = RootKey.CreateSubKey(BrowserEmulationKey, RegistryKeyPermissionCheck.ReadWriteSubTree)使用 EmulationKeyEmulationKey.SetValue(Process.GetCurrentProcess().ProcessName & ".exe", CType(Emulation, Integer), RegistryValueKind.DWord)结束使用结束使用结束子Public Shared Sub SetActiveXObjectCaching(ByVal Root As RegistryRoot, ByVal Enabled As Boolean)使用 RootKey 作为 RegistryKey = Root.RootDim ObjectCachingKey As RegistryKey = RootKey.OpenSubKey(ActiveXObjectCachingKey, True)如果 ObjectCachingKey 是什么,那么 ObjectCachingKey = RootKey.CreateSubKey(ActiveXObjectCachingKey, RegistryKeyPermissionCheck.ReadWriteSubTree)使用 ObjectCachingKeyObjectCachingKey.SetValue(Process.GetCurrentProcess().ProcessName & ".exe", CType(If(Enabled, 1, 0), Integer), RegistryValueKind.DWord)结束使用结束使用结束子Public NotInheritable 类 RegistryRoot私有 _root 作为 RegistryKey公共只读属性根作为 RegistryKey得到返回_root结束获取最终财产公共共享只读属性 HKEY_LOCAL_MACHINE 作为 RegistryRoot得到返回新的 RegistryRoot(Registry.LocalMachine)结束获取最终财产公共共享只读属性 HKEY_CURRENT_USER 作为 RegistryRoot得到返回新的 RegistryRoot(Registry.CurrentUser)结束获取最终财产Private Sub New(ByVal Root As RegistryKey)Me._root = 根结束子结束类结束类

要使用它,将一行放在应用程序的Startup事件中:

InternetExplorer.SetLatestBrowserEmulation(InternetExplorer.RegistryRoot.HKEY_LOCAL_MACHINE)如果您不想以管理权限运行您的应用程序,建议使用HKEY_CURRENT_USER".InternetExplorer.SetLatestBrowserEmulation(InternetExplorer.RegistryRoot.HKEY_CURRENT_USER)

(注意:使用 HKEY_LOCAL_MACHINE 根需要管理权限)

InternetExplorer.SetLatestBrowserEmulation() 方法将在指定的注册表根目录中将您的应用程序的浏览器模拟设置为 最新安装的 Internet Explorer 版本.>

但是,使用 InternetExplorer.SetBrowserEmulation() 方法,您可以手动控制它应该使用的 IE 版本(不推荐!).

阅读更多:

<小时>

编辑

我似乎根本无法进入该站点,但从我所读到的WebBrowser 控件中托管的 Flash 存在问题.

您可以尝试禁用ActiveX 对象缓存功能,这显然会导致 Flash 控件出现一些问题.

我更新了上面的 InternetExplorer 类.复制粘贴,然后将此行添加到应用程序的启动事件中:

InternetExplorer.SetActiveXObjectCaching(InternetExplorer.RegistryRoot.HKEY_CURRENT_USER, False)

如果它仍然不起作用,那么恐怕你不走运了.我找不到其他有用的东西.

I'm using the WebBrowser control in Visual Studio 2010 and trying to display the page: http://lk21.org.

Inside that webpage there are lots of scripts loaded, and it works fine if I open it through a web browser such as Firefox, Chrome, and the latest version of IE.

My question is, why does it display "Bad Request" when I tried using the WebBrowser component to navigate to that page?

Check this out:


UPDATE:

The page loads nicely using Visual Vincent's answer.

However the flash videos on the website (or I think it's something similar to flash) is unable to be played. See the comparison in the images below.

The strange thing is, if I open YouTube the flash works well. After a bit research, it seems to be caused by something else. Any clue how to solve it?

Internet Explorer - works fine:

WebBrowser control - for some reason the video is stuck and can't be played:

解决方案

It has probably got to do with that the WebBrowser control by default uses a document emulation mode of IE 7, meaning all pages are handled using the Internet Explorer 7 engine. Since that version is quite old most websites today are not compatible with it, which affects the functionality when you visit the page.

You can change this behaviour by adding a value for your application in the registry key Software\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION in either the HKEY_LOCAL_MACHINE hive or HKEY_CURRENT_USER. By doing so you are forcing your application to use a specific version of the IE engine.

I've written a class which will help you with this:

'A class for changing the WebBrowser control's document emulation.
'Written by Visual Vincent, 2017.

Imports Microsoft.Win32
Imports System.Security
Imports System.Windows.Forms

Public NotInheritable Class InternetExplorer
    Private Sub New()
    End Sub

    Public Const InternetExplorerRootKey As String = "Software\Microsoft\Internet Explorer"
    Public Const BrowserEmulationKey As String = InternetExplorerRootKey & "\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION"
    Public Const ActiveXObjectCachingKey As String = InternetExplorerRootKey & "\MAIN\FeatureControl\FEATURE_OBJECT_CACHING"

    Private Shared ReadOnly WebBrowserInstance As New WebBrowser 'Used to get the current IE version in a .NET-friendly manner.

    Public Enum BrowserEmulation As Integer
        IE7 = 7000
        IE8 = 8000
        IE8Standards = 8888
        IE9 = 9000
        IE9Standards = 9999
        IE10 = 10000
        IE10Standards = 10001
        IE11 = 11000
        IE11Edge = 11001
    End Enum

    Public Shared Sub SetLatestBrowserEmulation(ByVal Root As RegistryRoot)
        Dim Emulation As BrowserEmulation = BrowserEmulation.IE7
        Select Case WebBrowserInstance.Version.Major
            Case Is >= 11 : Emulation = BrowserEmulation.IE11Edge
            Case 10 : Emulation = BrowserEmulation.IE10Standards
            Case 9 : Emulation = BrowserEmulation.IE9Standards
            Case 8 : Emulation = BrowserEmulation.IE8Standards
        End Select
        InternetExplorer.SetBrowserEmulation(Root, Emulation)
    End Sub

    Public Shared Sub SetBrowserEmulation(ByVal Root As RegistryRoot, ByVal Emulation As BrowserEmulation)
        Using RootKey As RegistryKey = Root.Root
            Dim EmulationKey As RegistryKey = RootKey.OpenSubKey(BrowserEmulationKey, True)
            If EmulationKey Is Nothing Then EmulationKey = RootKey.CreateSubKey(BrowserEmulationKey, RegistryKeyPermissionCheck.ReadWriteSubTree)

            Using EmulationKey
                EmulationKey.SetValue(Process.GetCurrentProcess().ProcessName & ".exe", CType(Emulation, Integer), RegistryValueKind.DWord)
            End Using
        End Using
    End Sub

    Public Shared Sub SetActiveXObjectCaching(ByVal Root As RegistryRoot, ByVal Enabled As Boolean)
        Using RootKey As RegistryKey = Root.Root
            Dim ObjectCachingKey As RegistryKey = RootKey.OpenSubKey(ActiveXObjectCachingKey, True)
            If ObjectCachingKey Is Nothing Then ObjectCachingKey = RootKey.CreateSubKey(ActiveXObjectCachingKey, RegistryKeyPermissionCheck.ReadWriteSubTree)

            Using ObjectCachingKey
                ObjectCachingKey.SetValue(Process.GetCurrentProcess().ProcessName & ".exe", CType(If(Enabled, 1, 0), Integer), RegistryValueKind.DWord)
            End Using
        End Using
    End Sub

    Public NotInheritable Class RegistryRoot
        Private _root As RegistryKey

        Public ReadOnly Property Root As RegistryKey
            Get
                Return _root
            End Get
        End Property

        Public Shared ReadOnly Property HKEY_LOCAL_MACHINE As RegistryRoot
            Get
                Return New RegistryRoot(Registry.LocalMachine)
            End Get
        End Property

        Public Shared ReadOnly Property HKEY_CURRENT_USER As RegistryRoot
            Get
                Return New RegistryRoot(Registry.CurrentUser)
            End Get
        End Property

        Private Sub New(ByVal Root As RegistryKey)
            Me._root = Root
        End Sub
    End Class
End Class

To use it, put one of these lines in the application's Startup event:

InternetExplorer.SetLatestBrowserEmulation(InternetExplorer.RegistryRoot.HKEY_LOCAL_MACHINE)

'HKEY_CURRENT_USER is recommended if you do not want to run your application with administrative privileges.
InternetExplorer.SetLatestBrowserEmulation(InternetExplorer.RegistryRoot.HKEY_CURRENT_USER)

(NOTE: Using the HKEY_LOCAL_MACHINE root requires administrative privileges)

The InternetExplorer.SetLatestBrowserEmulation() method will set the browser emulation for your application, in the specified registry root, to the latest INSTALLED version of Internet Explorer.

However using the InternetExplorer.SetBrowserEmulation() method you can manually control what IE version it should use (not recommended!).

Read more:


EDIT

I seem to be unable to enter that site at all, but from what I've read there have been problems with Flash hosted in the WebBrowser control.

What you can try is to disable the ActiveX Object Caching feature, which can apparently cause some issues for Flash controls.

I updated the above InternetExplorer class. Copy-paste it, then add this line to your application's startup event:

InternetExplorer.SetActiveXObjectCaching(InternetExplorer.RegistryRoot.HKEY_CURRENT_USER, False)

If it still doesn't work then I'm afraid you're out of luck. I have not been able to find anything else that is useful.

这篇关于网页在 IE、Chrome 和 Firefox 中有效,但在使用 .NET WebBrowser 控件时无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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