我在Visual Studio 2010中使用WebBrowser控件并尝试显示页面:http://lk21.org。
在该网页内部加载了大量脚本,如果我通过Firefox,Chrome和最新版IE浏览器打开它,它可以正常工作。
我的问题是,为什么会显示"错误请求"当我尝试使用WebBrowser组件导航到该页面时?
检查出来:
更新
使用Visual Vincent的答案很好地加载页面。
然而,网站上的Flash视频(或者我认为它类似于闪存)无法播放。请参阅下图中的比较。
奇怪的是,如果我打开YouTube,闪光灯效果很好。经过一番研究,似乎是由其他东西造成的。有任何线索如何解决?
Internet Explorer - 运行正常:
WebBrowser控件 - 出于某种原因,视频被卡住而无法播放:
答案 0 :(得分:3)
默认情况下,WebBrowser
控件默认使用IE 7的文档模拟模式,这意味着所有页面都是使用Internet Explorer 7引擎处理的。由于该版本相当陈旧,目前大多数网站都与它不兼容,这会影响您访问该页面时的功能。
您可以通过在Software\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION
配置单元或HKEY_LOCAL_MACHINE
中的注册表项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
要使用它,请在应用程序的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)
(注意:使用HKEY_LOCAL_MACHINE
root需要管理员权限)
InternetExplorer.SetLatestBrowserEmulation()
方法会将您指定的注册表根目录中的应用程序的浏览器模拟设置为Internet Explorer的最新安装版本。
然而,使用InternetExplorer.SetBrowserEmulation()
方法,您可以手动控制应使用的IE版本 (不推荐!) 。
了解详情:
我似乎无法进入该网站,但是从我读过的内容there have been problems with Flash hosted in the WebBrowser control开始。
您可以尝试禁用ActiveX Object Caching feature,这显然会导致Flash控件出现问题。
我更新了上面的InternetExplorer
课程。复制粘贴它,然后将此行添加到应用程序的启动事件中:
InternetExplorer.SetActiveXObjectCaching(InternetExplorer.RegistryRoot.HKEY_CURRENT_USER, False)
如果它仍然不起作用那么我恐怕你运气不好。我找不到任何有用的东西。