简体   繁体   English

如何测试用ASP.Net编写的网站是否仍然存在?

[英]How to test if web site written in ASP.Net still alive?

I need to write a simple WinForms apps that can be fired to test if a website is still alive and that that website is able to read from a database. 我需要编写一个简单的WinForms应用程序,可以将其启动以测试网站是否仍然存在并且该网站能够从数据库读取。

I am using the whole "(HttpWebResponse)myHttpWebRequest.GetResponse()" thing in c# to test whether the site is alive, but I am at a lose for how to get a test page in my website to write something to the "Response" to indicate that it was able to test it's own connectivity to the database. 我正在使用c#中的整个“(HttpWebResponse)myHttpWebRequest.GetResponse()”东西来测试网站是否还活着,但是我对如何在我的网站中获取测试页以向“响应”中写一些内容感到困惑表示它能够测试自己与数据库的连接。

Here is the sample code for my Winforms side (ripped from the MSDN): 这是我的Winforms方面的示例代码(从MSDN中复制):

private void CheckUrl()
{
  try
  {
    HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.google.com");

    HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
    myHttpWebResponse.Close();

    label1.Text = myHttpWebRequest.Address.AbsoluteUri;
  }
  catch (WebException e)
  {
    label1.Text = "This program is expected to throw WebException on successful run." +
                                    "\n\nException Message :" + e.Message;

    if (e.Status == WebExceptionStatus.ProtocolError)
    {
      label1.Text = String.Format("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
      label2.Text =String.Format("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
    }
  }
  catch (Exception e)
  {
    label1.Text = e.Message;
  }
}

I was hoping for some help on the webform side of things to return to the above code. 我希望在Webform方面获得一些帮助,以返回上述代码。

Thanks for any help that you folks can provide. 感谢您提供的任何帮助。

  • Richard 理查德

You can create a webservice inside of the project called IAMALIVE and have it return a single char. 您可以在名为IAMALIVE的项目中创建一个Web服务,并使其返回单个字符。

On your WinForms area, consume said WebService and if it works, your site is alive. 在您的WinForms区域中,使用所说的WebService,如果它可以工作,则您的站点仍然有效。

In the essence of Papuccino's answer: you can actually create web services that are embedded in the C# code-behind of your WebForms pages by marking them with the [WebMethod] attribute. 从Papuccino的本质上讲,您实际上可以通过将WebForms页面标记为[WebMethod]属性来创建嵌入在WebForms页面背后的C#代码中的Web服务。 Those will reside within the web application, not just the server. 这些将驻留在Web应用程序中,而不仅仅是服务器中。

What happens when your site fails? 您的网站出现故障时会怎样? Does it return a 500 status code or timeout? 它是否返回500状态代码或超时?

Another way to look at it: does it always do something expected if it succeeds? 另一种看待它的方法:如果成功,它是否总是做预期的事情?

You might call a URL in your web app that you know will either return a 200 response code or will have some expected HTML markup in the response if things are working fine. 您可能会在Web应用程序中调用一个URL,您知道该URL将返回200响应代码,或者在一切正常的情况下在响应中具有一些预期的HTML标记。

Have your winform call this URL and examine the Response.status or the text in the output buffer for your expected markup. 让您的winform调用此URL,并检查Response.status或输出缓冲区中的文本以获取预期的标记。 You should also create a timeout in your httprequest. 您还应该在httprequest中创建一个超时。 If the page does not load within the timeout, you will get a web exception and you will know the site is failing. 如果未在超时时间内加载页面,则将出现网络异常,并且您将知道站点正在失败。

Also, if you have the budget, there are external monitoring services like gomez.com that can automate this and provide reporting on site availability. 另外,如果您有预算,则可以使用诸如gomez.com之类的外部监视服务来自动执行此操作,并提供有关站点可用性的报告。

have your webform page open a database connection and perform something simple/low-impact, eg 让您的Webform页面打开数据库连接并执行简单/低影响的操作,例如

select SystemTableId from dbo.[SystemTable] where SystemTableId = 1

where SystemTable is a single-row table. 其中SystemTable是单行表。

If the page throws an exception for any reason, Response.Write the exception message, otherwise Response.Write("SUCCESS") or similar. 如果页面由于任何原因引发异常,请Response.Write异常消息,否则Response.Write(“ SUCCESS”)或类似内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM