1

I need to show some page in System.Windows.Controls.WebBrowser and if there is a problem with the page (server is down, page is absent, connection is broken) the control should be hidden and nice looking error message with the "report an issue" prompt should be shown instead of the ugly IE's page with error.

But I can't figure out how to detect loading error. Even if server isn't available WebBrowser raises both Navigated and LoadCompleted events. And even if page is successfully shown Navigated and LoadCompleted are raised with the null in all there NavigationEventArgs's properties.

Is there a way to determine when WebBrowser fail to load the page?

Ilya Serbis
  • 21,149
  • 6
  • 87
  • 74

1 Answers1

-1

One way to manage with this is to perform independent HTTP GET request before Navigating to the page using WebBrowser just to check the page is available:

string url = "http://site.domain.com/somepage";
try
{
    WebRequest request = WebRequest.Create(url);
    HttpWebResponse response = (HttpWebResponse) request.GetResponse();

    if (response.StatusCode != HttpStatusCode.OK)
    {
        throw new InvalidOperationException()
    }

    // page is available. Load it to the WebBrowser and show it to the user
    webBrowser.Naviage(url);
}
catch (Exception exception)
{
    // page isn't available - show an error screen
}

Update:

All the other approaches I've found:

Community
  • 1
  • 1
Ilya Serbis
  • 21,149
  • 6
  • 87
  • 74