tags:

views:

124

answers:

3

I am using the WebBrowser in WPF like this:

 <WebBrowser 
     Name="SSSBrowser" 
     Margin="8,4,8,8" 
     Grid.Row="1"            
     dp:WebBrowserUtility.BindableSource="{Binding WebAddress}"/>

And in C# I am loading it simply, for now, like this:

private string _webAddress;
public string WebAddress 
{
  get { return "http://www.somewebsite.com/updates/message/message.htm"; }
  set { _webAddress = value; }
}

What I would like to do is prevent it from displaying an error if they cannot reach the webpage, for whatever reason.

How do I keep tell if the website returned an error in code and disable the WebBrowser so that it doesn't give an error on screen to user?

Any help would be greatly appreciated!

A: 

Not sure via WPF, but if you use HttpWebRequest and HttpWebResponse to programatically try to fetch your url first, the response will give you the http StatusCode, i.e. 200, 404 etc. Might be useful if you want to check for a 200 first and disable the browser preemptively. Not exactly the answer to your question, but a possibility.

RandomNoob
A: 

There doesn't seem to be an ErrorOpeningURL event for the WPF WebBrowser object, so, judging by previous experience, you could wire up to the Navigated event and check whether the URI is the IE error page (res://Error.html.. IIRC) or dig into the NavigationEventArgs for the WebResponse and check the headers.

To suppress the error, hide the WebBrowser component, perhaps by covering with a polite message - your user will want to know that the operation did not succeed, but doesn't have to see the navigation FAIL.

JBRWilkinson
I feel as if I am close with this option. I have ' private void SSSBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)' at this point but I'm not sure where I'm looking to verify there was a problem. Any suggestions?
ErocM
A: 

There is no direct way to do this

But you can do a trick.

Create a property in your view model called 'IsHostFound' of bool type and then ping asynchronously your desired URL , if you got the response then set 'IsHostFound' to true.

See below URL for pinging asynchronously.

http://www.geekpedia.com/tutorial234_Asynchronous-Ping-using-Csharp.html

saurabh
I want to grab page errors, not host errors. Thanks though.
ErocM