2010-11-19 109 views
10

我在我的winform上有WebBrowser控件。當我嘗試瀏覽到一些網址我得到標準的IE錯誤頁面,如:WebBrowser控件頁面加載錯誤

  • 「導航到網頁已取消」
  • 「地址是無效」
  • 「頁面無法裝」

我需要處理這些錯誤,並返回自定義錯誤消息給用戶。有什麼辦法可以解決這個問題嗎?

回答

10

你要處理的NavigateError事件如圖here

編輯:包括鏈接示例代碼:

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); 
    } 
} 
+0

我會閱讀例子並使用它。謝謝。還有另一個問題。當用戶嘗試導航到文件鏈接時(當用戶嘗試下載文件時),我需要處理這種情況 - 我應該拒絕這樣的請求。這種情況是否也可以解決? – 2010-11-19 14:22:55

+0

如果您還處理Navigate事件,則可以檢查URL(將成爲文件名),並將WebBrowserNavigatingEventArgs的Cancel屬性設置爲true(如果它是文件而不是頁面)。儘管如此,這還是取決於能否區分文件下載與常規導航。 – stuartd 2010-11-19 14:32:49

+0

那麼,我可以檢查我的代碼中的大部分文件擴展名(如果uri包含文件名)。但是有很多例子,當服務器可以響應某些流,這可以通過瀏覽器像二進制數據一樣檢測到,打開/保存/取消messagebox會顯示。我可以在將它展示給用戶之前捕獲這個消息框嗎? – 2010-11-19 14:44:34