开发者

How do i catch page cannot load message?

Hi i hope someone can help, In basic terms i am trying to stop the embedded browser, in my Windows Forms app, from navigating to the 'This program cannot display the webpage' page and instead display my own error page.

The C# application is a Web Browser embedded in my Windows Forms, its purpose is allowing the user to click on the provided buttons that开发者_如何学Go navigate the browser to predefined WebService URL's. If the Service is not running on the Machine then instead of the browser saying that it was unable to load i need to to navigate to my custom page instead.

I have looked around and as yet had no luck in finding a solution apparently HttpStatusCodes are a way to go but i have no idea how to use them.

Code Snippet:

private void currentMachineToolStripMenuItem_Click(object sender, EventArgs e)
{
   webBrowser1.Navigate("http://localhost:2021/wsservice/status");
}

As you can see, currently very simple program.


You can try this:

Private void WebBrowser1_NavigateError(Object sender, EventArgs e)
{
    WebBrowser1.Navigate( App.Path + "\retry.htm");
}


Taken from this link -

You would need to use an HttpWebRequest and an HttpWebResponse object to do this. Create the HttpWebRequest with the desired URL, using the WebRequest.Create(url) method. Then use the GetResponse() method to get the HttpWebResponse. The HttpWebResponse will have the status code returned from the server. This will tell you if the URL exists or not. 404 indicates "Page not Found." 200 indicates "Success." Etc.

http://msdn.microsoft.com/en-us/library/8y7x3zz2(vs.71).aspx

Also another link which might help you - http://www.vcskicks.com/check-website.php


You can check the page content for a word that indicates that the page did not load correctly

if(webBrowser1.Document.InnerHtml.Contains("error"))


You can check too the returned document url, find the resource address that webBrowser use to show error page:

if (webBrowser1.Document.Url.ToString().Contains("res://ieframe.dll/dnserrordiagoff.htm") )
    ...
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜