65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 HostTracker 测试 Web 主机可靠性

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2投票s)

2012年7月26日

CPOL

2分钟阅读

viewsIcon

17148

downloadIcon

180

定期访问已配置的网站,并记录连接情况。

引言

您的网站是否比您希望的更频繁地出现故障?您在联系支持时是否遇到推诿?HostTracker 提供了您可以发送给您的网站托管服务商的日志文件,以记录您的连接问题。 

背景

我编写 HostTracker 是为了解决我与网站托管服务商遇到的问题。

解决方案

HostTracker Visual Studio 2008 项目包含您需要的一切,以通过 Windows 服务记录连接情况。

配置非常简单。如下所示,Url1 是要测试的网站,Url2 用于比较目的,托管服务商的主网站是一个不错的选择。如果您的互联网连接失败,那么两个 Url 都会失败。但是,如果 Url2 通过而 Url1 失败,则可能表明您的网站托管服务商的服务器存在问题。

<configuration>
 <appSettings>
  <add key="TestMode" value="N" />
  <add key="Url1" value="http://godaddy.hugetiger.com/Time.aspx" />
  <add key="Url2" value="http://www.godaddy.com                " />
  <add key="WaitMillisecs" value="300000" />
 </appSettings>
</configuration>

测试和调试 Windows 服务可能很棘手。

为了简单起见,我在配置文件中包含一个 TestMode 变量,以便将软件作为易于调试的控制台应用程序运行,或者如下所示的 Windows 服务运行。

static void Main() 
{
 BasicUtil bu = new BasicUtil();
 bool bTest = bu.StringToBool(ConfigurationManager.AppSettings["TestMode"]);
 if (bTest)
 {
  Console.WriteLine("Start of Main (HostTracker)");
  TestHostTracker hw = new TestHostTracker();
  hw.TestOnStart();				
  Console.WriteLine("To run as a Service change TestMode in HostTracker.exe.config to N");
  Console.WriteLine("======== Press Key to end program");
  Console.ReadKey();
  hw.TestOnStop();
 }
 else
 {
  ServiceBase[] ServicesToRun = new ServiceBase[] { new HostTracker() };
  ServiceBase.Run(ServicesToRun);
 }
}

下面的 ProjectInstaller 类方法 InitializeComponent() 代码段在服务安装期间用于设置初始服务参数,这些参数可以通过服务控制面板图标进行更改。

this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;

this.HostTrackerInstaller.ServiceName = "HostTrackerServic";
this.HostTrackerInstaller.Description = "Visits 2 configured web sites every interval";
this.HostTrackerInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;

我从之前的项目获取了 ProjectInstaller.csProjectInstaller.Designer.cs。 似乎在某些情况下,这段代码是自动生成的。 但是,我只是复制了代码并根据需要进行了修改。

WebScrape 继承自 Webclient,方法 GetWebPage 如下所示,访问传递的 URL 并检索网页 html 以及完成此操作所需的时间跨度,并返回一个布尔值以指示成功或失败。

public bool GetWebPage(String pUrl, String pPost, out String pPageHtml, out TimeSpan ts)
{
 pPageHtml = String.Empty;
 bool rv = false;
 objRequest = (HttpWebRequest)WebRequest.Create(pUrl);
 if (pPost == String.Empty)
 {
  objRequest.Method = "GET";
 }
 else
 {
  objRequest.Method = "POST";
 }
 objRequest.ContentLength = pPost.Length;
 objRequest.ContentType = "application/x-www-form-urlencoded";
 String s = @"Mozilla/4.0+(compatible;+MSIE+8.0;+Windows+NT+6.0;+Trident/4.0";
 s += @";+SLCC1;+.NET+CLR+2.0.50727;+.NET+CLR+1.1.4322;";
 s += @"+.NET+CLR+3.5.30729;+.NET+CLR+3.0.30729)";

 // the user agent identifies the web host about browser details, such as type and version
 // of browser application and operating system/
 objRequest.UserAgent = s;

 if (pPost != String.Empty)
 {
  try
  {
   myWriter = new StreamWriter(objRequest.GetRequestStream());
   myWriter.Write(pPost);
  }
  catch (Exception)
  {
   //WriteLine(e.Message);
  }
  finally
  {
   myWriter.Close();
  }
 }
 DateTime dtStart = DateTime.Now;
 HttpWebResponse objResponse = null;
 try
 {
  objResponse = (HttpWebResponse)objRequest.GetResponse();
 }
 catch (Exception)
 {
  ts = new TimeSpan();
  return rv;
 }
 DateTime dtFin = new DateTime();

 //Check out the html.
 Stream aStream = objResponse.GetResponseStream();
 if (aStream != null)
 {
  using (StreamReader sr = new StreamReader(aStream))
  {
   pPageHtml = sr.ReadToEnd(); // page html goes to string pPageHtml
   dtFin = DateTime.Now;
   sr.Close();
   rv = true;
  }
 }
 ts = dtFin - dtStart;
 return rv;
}

安装服务

要安装,请使用:InstallUtil.exe     HostTracker.exe

要卸载,请使用:InstallUtil.exe /u HostTracker.exe

或者使用下面的 InstallService.bat

rem uninstall HostTracker
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /u "C:\path\HostTracker.exe"
rem install   HostTracker
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe    "C:\path\HostTracker.exe"
pause

您需要编辑 InstallService.bat 中的路径

关注点

HostTracker 的编写风格是我喜欢并且认为适合小型项目和独立程序员的风格。 其他组织风格可能更适合团队编程或大型项目。

编写这个软件、记录它以及为 codeproject.com 撰写这篇文章很有趣。

在 Windows 7 和 MS Server 2003 上测试过。

历史

First version.

© . All rights reserved.