最近在写一个小工具来监控网站是否正常
代码如下:
public void MonitorWeb(Model.ServiceInfo mServerInfo)
{
var sUrl = mServerInfo.ServiceConfig;
var mLogInfo = new Model.LogInfo { ServiceId = mServerInfo.ServiceId };
try
{
var mWebRequest = (HttpWebRequest)WebRequest.Create(sUrl);
var mWebResponse = (HttpWebResponse)mWebRequest.GetResponse();
if (mWebResponse.StatusCode == HttpStatusCode.OK)
{
mLogInfo.Status = "1";
mLogInfo.Remark = "";
mServerInfo.ErrorCou = 0;
}
else
{
mLogInfo.Status = "0";
mLogInfo.Remark = mWebResponse.StatusDescription;
mServerInfo.ErrorCou = mServerInfo.ErrorCou + 1;
}
mWebResponse.Close();
}
catch (Exception ex)
{
mLogInfo.Status = "0";
mLogInfo.Remark = ex.Message;
mServerInfo.ErrorCou = mServerInfo.ErrorCou + 1;
}
DAL.DbOp.UpdateState(mServerInfo, mLogInfo);
}
{
var sUrl = mServerInfo.ServiceConfig;
var mLogInfo = new Model.LogInfo { ServiceId = mServerInfo.ServiceId };
try
{
var mWebRequest = (HttpWebRequest)WebRequest.Create(sUrl);
var mWebResponse = (HttpWebResponse)mWebRequest.GetResponse();
if (mWebResponse.StatusCode == HttpStatusCode.OK)
{
mLogInfo.Status = "1";
mLogInfo.Remark = "";
mServerInfo.ErrorCou = 0;
}
else
{
mLogInfo.Status = "0";
mLogInfo.Remark = mWebResponse.StatusDescription;
mServerInfo.ErrorCou = mServerInfo.ErrorCou + 1;
}
mWebResponse.Close();
}
catch (Exception ex)
{
mLogInfo.Status = "0";
mLogInfo.Remark = ex.Message;
mServerInfo.ErrorCou = mServerInfo.ErrorCou + 1;
}
DAL.DbOp.UpdateState(mServerInfo, mLogInfo);
}
最重要部份
var mWebRequest = (HttpWebRequest)WebRequest.Create(sUrl);
var mWebResponse = (HttpWebResponse)mWebRequest.GetResponse();
if (mWebResponse.StatusCode == HttpStatusCode.OK)
var mWebResponse = (HttpWebResponse)mWebRequest.GetResponse();
if (mWebResponse.StatusCode == HttpStatusCode.OK)