网页在 IE、Chrome 和 Firefox 中有效,但在使用 .NET WebBrowser 控件时无效

gum*_*ruh 2 vb.net webbrowser-control visual-studio-2010 visual-studio-2012

我在 Visual Studio 2010 中使用 WebBrowser 控件并尝试显示页面:http://lk21.org

在该网页中加载了大量脚本,如果我通过 Firefox、Chrome 和最新版本的 IE 等 Web 浏览器打开它,它就可以正常工作。

我的问题是,当我尝试使用 WebBrowser 组件导航到该页面时,为什么会显示“错误请求”?

看一下这个:

在此处输入图片说明


更新:

使用 Visual Vincent 的答案可以很好地加载页面。

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

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

Internet Explorer - 工作正常:

在此处输入图片说明

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

在此处输入图片说明

Vis*_*ent 5

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

您可以Software\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION通过在HKEY_LOCAL_MACHINEhive 或HKEY_CURRENT_USER. 通过这样做,您将强制您的应用程序使用特定版本的 IE 引擎。

我写了一个课程来帮助你解决这个问题:

'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
Run Code Online (Sandbox Code Playgroud)

要使用它,把一个应用程序中的这些行的Startup事件:

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)
Run Code Online (Sandbox Code Playgroud)

注意:使用HKEY_LOCAL_MACHINEroot 需要管理权限)

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

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

阅读更多:


编辑

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

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

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

InternetExplorer.SetActiveXObjectCaching(InternetExplorer.RegistryRoot.HKEY_CURRENT_USER, False)
Run Code Online (Sandbox Code Playgroud)

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