VB.NET Webbrowser System.UnauthorizedAccessException在循环中

问题描述

| 我已经将该代码运行了至少一年,今天却抛出了一个例外,我一直无法弄清它为什么会发生。它是一个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     

解决方法

        该网站在ad.doubleclick.net上有一个框架,默认情况下,Internet区域禁止跨域框架访问,因此会出现安全异常。 捕获异常并继续。您无需担心框架中的所有内容,doubleclick是一项广告服务。 您可以实现IInternetSecurityManager并让IE相信ad.doubleclick.net和FlightAware.com是同一网站,但是如果将信任范围扩展到任意网站,则可能导致安全问题。     ,        这是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);
}