如何测试 ASP.Net 编写的网站是否还活着?

发布于 2024-08-04 19:08:55 字数 1188 浏览 5 评论 0原文

我需要编写一个简单的 WinForms 应用程序,可以触发该应用程序来测试网站是否仍然存在以及该网站是否能够从数据库中读取数据。

我正在使用 C# 中的整个“(HttpWebResponse)myHttpWebRequest.GetResponse()”来测试该网站是否还活着,但我不知道如何在我的网站中获取测试页面以向“响应”写入内容表明它能够测试它自己与数据库的连接。

以下是我的 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;
  }
}

我希望在 Webform 端获得一些帮助以返回到上面的代码。

感谢你们提供的任何帮助。

  • 理查德

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.

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.

Here is the sample code for my Winforms side (ripped from the 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.

Thanks for any help that you folks can provide.

  • Richard

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

救赎№ 2024-08-11 19:08:55

您可以在名为 IAMALIVE 的项目内部创建一个 Web 服务,并让它返回单个字符。

在您的 WinForms 区域中,使用所述 WebService,如果它有效,则您的网站处于活动状态。

You can create a webservice inside of the project called IAMALIVE and have it return a single char.

On your WinForms area, consume said WebService and if it works, your site is alive.

束缚m 2024-08-11 19:08:55

Papuccino 答案的本质是:您实际上可以通过使用 [WebMethod] 属性标记 Web 服务,从而创建嵌入 WebForms 页面 C# 代码隐藏中的 Web 服务。这些将驻留在 Web 应用程序中,而不仅仅是服务器中。

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. Those will reside within the web application, not just the server.

天暗了我发光 2024-08-11 19:08:55

当您的网站出现故障时会发生什么?它是否返回 500 状态代码或超时?

另一种看待它的方式是:如果它成功了,它是否总是会做一些预期的事情?

您可以在 Web 应用程序中调用一个 URL,您知道该 URL 要么返回 200 响应代码,要么在响应中包含一些预期的 HTML 标记(如果一切正常)。

让您的 winform 调用此 URL 并检查 Response.status 或输出缓冲区中的文本是否有您期望的标记。您还应该在 httprequest 中创建超时。如果页面在超时时间内未加载,您将收到网络异常,并且您将知道该网站出现故障。

此外,如果您有预算,gomez.com 等外部监控服务可以自动执行此操作并提供有关站点可用性的报告。

What happens when your site fails? Does it return a 500 status code or timeout?

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.

Have your winform call this URL and examine the Response.status or the text in the output buffer for your expected markup. You should also create a timeout in your 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.

久隐师 2024-08-11 19:08:55

让您的 Web 表单页面打开数据库连接并执行一些简单/低影响的操作,例如,

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

其中 SystemTable 是单行表。

如果页面因任何原因引发异常,则 Response.Write 异常消息,否则 Response.Write("SUCCESS") 或类似内容。

have your webform page open a database connection and perform something simple/low-impact, e.g.

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

where SystemTable is a single-row table.

If the page throws an exception for any reason, Response.Write the exception message, otherwise Response.Write("SUCCESS") or similar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文