WebBrowser控件页面加载错误 [英] WebBrowser control page load error

查看:166
本文介绍了WebBrowser控件页面加载错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的WinForm WebBrowser控件。当我尝试浏览到一些网站我得到标准的IE错误页面,如:

I have WebBrowser control on my winform. When I try navigate to some web-site I get standard IE error pages like:

  • 在导航到网页已取消
  • 在地址是无效
  • 在页面无法加载

我需要处理这些错误,并返回自定义错误信息给用户。请问有什么办法可以解决这个问题呢?

I need to handle these errors and return custom error message to user. Is there any way to solve this issue?

推荐答案

您要处理的NavigateError事件如<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.createsink%28VS.80%29.aspx"相对=nofollow>这里

You want to handle the NavigateError event as shown here

编辑:包括链路例如code:

including example code from the link:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Security.Permissions;

namespace WebBrowserExtensibility
{
    [PermissionSetAttribute(SecurityAction.Demand, Name="FullTrust")]
    public class Form1 : Form
    {
        [STAThread]
        public static void Main()
        {
            Application.Run(new Form1());
        }

        private WebBrowser2 wb = new WebBrowser2();
        public Form1()
        {
            wb.Dock = DockStyle.Fill;
            wb.NavigateError += new 
                WebBrowserNavigateErrorEventHandler(wb_NavigateError);
            Controls.Add(wb);

            // Attempt to navigate to an invalid address.
            wb.Navigate("www.widgets.microsoft.com");
        }

        private void wb_NavigateError(
            object sender, WebBrowserNavigateErrorEventArgs e)
        {
            // Display an error message to the user.
            MessageBox.Show("Cannot navigate to " + e.Url);
        }
    }

    public class WebBrowser2 : WebBrowser
    {
        AxHost.ConnectionPointCookie cookie;
        WebBrowser2EventHelper helper;

        [PermissionSetAttribute(SecurityAction.LinkDemand, Name="FullTrust")]
        protected override void CreateSink()
        {
            base.CreateSink();

            // Create an instance of the client that will handle the event
            // and associate it with the underlying ActiveX control.
            helper = new WebBrowser2EventHelper(this);
            cookie = new AxHost.ConnectionPointCookie(
                this.ActiveXInstance, helper, typeof(DWebBrowserEvents2));
        }

        [PermissionSetAttribute(SecurityAction.LinkDemand, Name="FullTrust")]
        protected override void DetachSink()
        {
            // Disconnect the client that handles the event
            // from the underlying ActiveX control.
            if (cookie != null)
            {
                cookie.Disconnect();
                cookie = null;
            }
            base.DetachSink();
        }

        public event WebBrowserNavigateErrorEventHandler NavigateError;

        // Raises the NavigateError event.
        protected virtual void OnNavigateError(
            WebBrowserNavigateErrorEventArgs e)
        {
            if (this.NavigateError != null)
            {
                this.NavigateError(this, e);
            }
        }

        // Handles the NavigateError event from the underlying ActiveX 
        // control by raising the NavigateError event defined in this class.
        private class WebBrowser2EventHelper : 
            StandardOleMarshalObject, DWebBrowserEvents2
        {
            private WebBrowser2 parent;

            public WebBrowser2EventHelper(WebBrowser2 parent)
            {
                this.parent = parent;
            }

            public void NavigateError(object pDisp, ref object url, 
                ref object frame, ref object statusCode, ref bool cancel)
            {
                // Raise the NavigateError event.
                this.parent.OnNavigateError(
                    new WebBrowserNavigateErrorEventArgs(
                    (String)url, (String)frame, (Int32)statusCode, cancel));
            }
        }
    }

    // Represents the method that will handle the WebBrowser2.NavigateError event.
    public delegate void WebBrowserNavigateErrorEventHandler(object sender, 
        WebBrowserNavigateErrorEventArgs e);

    // Provides data for the WebBrowser2.NavigateError event.
    public class WebBrowserNavigateErrorEventArgs : EventArgs
    {
        private String urlValue;
        private String frameValue;
        private Int32 statusCodeValue;
        private Boolean cancelValue;

        public WebBrowserNavigateErrorEventArgs(
            String url, String frame, Int32 statusCode, Boolean cancel)
        {
            urlValue = url;
            frameValue = frame;
            statusCodeValue = statusCode;
            cancelValue = cancel;
        }

        public String Url
        {
            get { return urlValue; }
            set { urlValue = value; }
        }

        public String Frame
        {
            get { return frameValue; }
            set { frameValue = value; }
        }

        public Int32 StatusCode
        {
            get { return statusCodeValue; }
            set { statusCodeValue = value; }
        }

        public Boolean Cancel
        {
            get { return cancelValue; }
            set { cancelValue = value; }
        }
    }

    // Imports the NavigateError method from the OLE DWebBrowserEvents2 
    // interface. 
    [ComImport, Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"),
    InterfaceType(ComInterfaceType.InterfaceIsIDispatch),
    TypeLibType(TypeLibTypeFlags.FHidden)]
    public interface DWebBrowserEvents2
    {
        [DispId(271)]
        void NavigateError(
            [In, MarshalAs(UnmanagedType.IDispatch)] object pDisp,
            [In] ref object URL, [In] ref object frame, 
            [In] ref object statusCode, [In, Out] ref bool cancel);
    }
}

这篇关于WebBrowser控件页面加载错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆