开发者

VB.NET Webbrowser System.UnauthorizedAccessException in Loop

开发者 https://www.devze.com 2023-03-10 00:57 出处:网络
I\'ve had this code working for at least a year and today it threw an exception that i haven\'t been able to figure out why its happening. Its a Forms.WebBrowser that hits a generic site first and the

I've had this code working for at least a year and today it threw an exception that i haven't been able to figure out why its happening. Its a Forms.WebBrowser that hits a generic site first and then a secondary site.

        'first site
        wbr.ScriptErrorsSuppressed = False
        wbr.Navigate("http://www.bing.com/?rb=0")
        Do
            Application.DoEvents()
        Loop Until wbr.ReadyState = WebBrowserR开发者_Python百科eadyState.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

The error happens on the second site access and it shows that it errors on the very last line with

System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

at System.Windows.Forms.UnsafeNativeMethods.IHTMLLocation.GetHref() at System.Windows.Forms.WebBrowser.get_Document() at System.Windows.Forms.WebBrowser.get_ReadyState()

I just don't get why its errorring on the second site and not the first and what exactly that error message means. I've looked at some help forums but nothing concrete that i can use to troubleshoot.

AGP


The web site has a frame on ad.doubleclick.net, by default cross-domain frame access is disabled for the internet zone, so you get a security exception.

Catch the exception and move on. There isn't much you need to care about in the frame, doubleclick is an ad service.

You can implement IInternetSecurityManager and let IE to believe ad.doubleclick.net and FlightAware.com are the same web site, but this can cause security problem if you extend the trust to arbitrary web sites.


Here is a little hack in C# which you can convert in 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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号