我已经有这个代码工作了至少一年,今天它抛出了一个例外,我无法弄清楚它为什么会发生。它是一个Forms.WebBrowser,它首先命中一个通用站点,然后命中一个辅助站点。
'first site
wbr.ScriptErrorsSuppressed = False
wbr.Navigate("http://www.bing.com/?rb=0")
Do
Application.DoEvents()
Loop Until wbr.ReadyState = WebBrowserReadyState.Complete
'second site
wbr.ScriptErrorsSuppressed = True
Dim start As DateTime = DateTime.Now
Dim loopTimeout As TimeSpan = TimeSpan.FromSeconds(timeout)
wbr.Navigate("http://www.FlightAware.com")
Do
Application.DoEvents()
'loop timer
If DateTime.Now.Subtract(start) > loopTimeout Then
'stop browser
wbr.Stop()
'throw exception
Dim eExpTme As Exception = New Exception("A loop timeout occurred in the web request.")
Throw eExpTme
End If
Loop Until wbr.ReadyState = WebBrowserReadyState.Complete
错误发生在第二次站点访问时,它显示在最后一行
时出错System.UnauthorizedAccessException:访问被拒绝。 (HRESULT异常:0x80070005(E_ACCESSDENIED))
在System.Windows.Forms.UnsafeNativeMethods.IHTMLLocation.GetHref() 在System.Windows.Forms.WebBrowser.get_Document() 在System.Windows.Forms.WebBrowser.get_ReadyState()
我只是不明白为什么它在第二个网站上的错误而不是第一个以及错误信息到底意味着什么。我已经看了一些帮助论坛,但没有什么具体的,我可以用来排除故障。
AGP
答案 0 :(得分:1)
该网站在ad.doubleclick.net上有一个框架,默认情况下cross-domain frame access为disabled for the internet zone,因此您会收到安全例外。
抓住异常并继续前进。您需要在框架中关注的不多,doubleclick是广告服务。
您可以实施IInternetSecurityManager并让IE相信ad.doubleclick.net和FlightAware.com是同一个网站,但如果您将信任扩展到任意网站,这可能会导致安全问题。
答案 1 :(得分:0)
这是C#中的一个小问题,您可以在Vb.net中进行转换:
public class CrossFrameIE
{
// Returns null in case of failure.
public static IHTMLDocument2 GetDocumentFromWindow(IHTMLWindow2 htmlWindow)
{
if (htmlWindow == null)
{
return null;
}
// First try the usual way to get the document.
try
{
IHTMLDocument2 doc = htmlWindow.document;
return doc;
}
catch (COMException comEx)
{
// I think COMException won't be ever fired but just to be sure ...
if (comEx.ErrorCode != E_ACCESSDENIED)
{
return null;
}
}
catch (System.UnauthorizedAccessException)
{
}
catch
{
// Any other error.
return null;
}
// At this point the error was E_ACCESSDENIED because the frame contains a document from another domain.
// IE tries to prevent a cross frame scripting security issue.
try
{
// Convert IHTMLWindow2 to IWebBrowser2 using IServiceProvider.
IServiceProvider sp = (IServiceProvider)htmlWindow;
// Use IServiceProvider.QueryService to get IWebBrowser2 object.
Object brws = null;
sp.QueryService(ref IID_IWebBrowserApp, ref IID_IWebBrowser2, out brws);
// Get the document from IWebBrowser2.
IWebBrowser2 browser = (IWebBrowser2)(brws);
return (IHTMLDocument2)browser.Document;
}
catch
{
}
return null;
}
private const int E_ACCESSDENIED = unchecked((int)0x80070005L);
private static Guid IID_IWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
private static Guid IID_IWebBrowser2 = new Guid("D30C1661-CDAF-11D0-8A3E-00C04FC9E26E");
}
// This is the COM IServiceProvider interface, not System.IServiceProvider .Net interface!
[ComImport(), ComVisible(true), Guid("6D5140C1-7436-11CE-8034-00AA006009FA"),
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IServiceProvider
{
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int QueryService(ref Guid guidService, ref Guid riid, [MarshalAs(UnmanagedType.Interface)] out object ppvObject);
}