2013-07-09 28 views
0

我有一个关于去网站的项目。如果网站无法加载,浏览器将显示一个页面,文字为:“此页面无法显示”。在这种情况下,我想自动刷新浏览器。程序如何检测到该网站无法加载? 。我试过Ping()检查连接,但似乎连接没问题。看看我的下面的代码:C# - 如何使用Watin检测网站是否返回包含内容的页面“该页面无法显示”?

public void exam() 
     { 
      var ie = new IE(); 
      ie.GoTo("http://search.yahoo.com"); 
      ie.WaitForComplete(5);    
      if (ie.ContainsText("This page cannot be displayed")) 
      { 
       ie.Close();// or ie.Refresh() 
      } 

     } 

它不起作用。帮帮我!

回答

0

我不知道你的IE类型是什么,但可以使用HttpWebRequest/HttpWebResponse并检查响应的StatusCode属性。

例如: -

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.google.com"); 
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
if (response.StatusCode == HttpStatusCode.NotFound) 
{ 
    //page not found 
} 
//etc.... 

HttpStatusCode该枚举可以是用于抗多种不同的状态的检查是有用的。

0

您的代码看起来很好,只需从等待完成中删除的时间 确保文本与浏览器中显示的文本相同。

public void exam() 
    { 
     var ie = new IE(); 
     ie.GoTo("http://search.yahoo.com"); 
     ie.WaitForComplete();    
     if (ie.ContainsText("This page cannot be displayed")) 
     { 
      ie.Refresh(); 
     } 

    }