Ping命令检测网站运行状态
生活随笔
收集整理的這篇文章主要介紹了
Ping命令检测网站运行状态
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Ping命令檢測網站運行狀態
最近,小編在項目中遇到一個問題,檢測服務上網站的運行狀況,其中,用到了Ping命令來測試
Ping的是什么?IP地址或者域名
DNS:正向解析,將域名轉換成IP地址,域名解析協議
RDNS:反向解析,將IP地址轉換成域名
主要包含三步
第一, 檢測本地網絡連接狀態
第二, 檢測與服務器的網絡連接狀態
第三, 檢測服務器上網站的運行狀態
代碼如下
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.NetworkInformation; using System.Text.RegularExpressions; using System.Web; using WebDetection.Model;namespace WebDetection {public class DetectionWeb{#region 檢測網站運行狀態/// <summary>/// 檢測網站運行狀態/// </summary>/// <returns></returns>public static bool CheckWeb(){// 獲取xml文件List<WebXMLmodel> list = OperateXML.readXML();foreach (WebXMLmodel listItem in list){// 獲取檢查網址string WebUrl = listItem.weburl.Trim();// 檢查網站的格式bool Format = FormatWebUrl(WebUrl);if (Format == false){listItem.httpdescription = "網站的網址格式不正確!";listItem.Webstate = "失敗";return true;}// 檢查與服務器的連接狀態bool Result = PingWebUrl(WebUrl);if (Result == false){listItem.httpdescription = "無法Ping通網站!";listItem.Webstate = "失敗";return true;}// 檢測網站的運行狀態string str = CheckWebStatus(WebUrl);// http的狀態碼string httpcode = str.Substring(0, 3);// http的返回值string httpresult = str.Substring(3);// http狀態碼詳情string httpdescription = Checkhttpcode(httpcode);// 循環遍歷賦值listItem.httpcode = httpcode;listItem.httpresult = httpresult;listItem.httpdescription = httpdescription;// 網站訪問是否成功if (httpcode == "200"){listItem.Webstate = "成功";listItem.ismail = "未發送";}else{listItem.Webstate = "失敗";}// 更新當前網站的狀態bool flag = OperateXML.writeXML(list);}return true;}#endregion#region 檢測當前網絡狀態/// <summary>/// 檢測當前網絡狀態/// </summary>/// <returns></returns>public static bool Ping(){bool PinhRight = false;try{// Ping百度Ping Ping = new Ping();string WebUrl = "www.baidu.com";PingReply ReCode = Ping.Send(WebUrl);// Ping 成功!if (ReCode.Status == IPStatus.Success){PinhRight = true;}return PinhRight;}catch{return PinhRight;}}#endregion#region 檢查網址格式是否正確/// <summary>/// 檢查網址格式是否正確/// </summary>/// <param name="WebUrl"></param>/// <returns></returns>public static bool FormatWebUrl(string WebUrl){bool result = false;if (WebUrl != null && WebUrl != ""){var strRegex = @"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&$%\$#\=~])*$";Regex re = new Regex(strRegex);if (re.IsMatch(WebUrl)){result = true;}}return result;}#endregion#region Ping網站網址/// <summary>/// Ping網站網址/// </summary>/// <param name="WebUrl"></param>/// <returns></returns>public static bool PingWebUrl(string WebUrl){// 移除所有空白字符WebUrl = WebUrl.Trim();// 判斷協議,http、httpsstring deal = WebUrl.ToLower().Substring(0, 5);// 網址截止位置int index;// 需要Ping的網址string PingWeb;// https協議if (deal == "https"){// 從第8位開始,搜索返回第一個"/"號出現的位置// 默認下標從0開始index = WebUrl.ToLower().IndexOf('/', 8);if (index == -1){// 從第8位開始,包含第8位,取到結尾PingWeb = WebUrl.Substring(8);}else{// 從第8位開始,取index-8個字符PingWeb = WebUrl.Substring(8, index-8);}}else{ // http協議// 從第7位開始,搜索返回第一個"/"號出現的位置// 默認下標從0開始index = WebUrl.ToLower().IndexOf('/', 7);if (index == -1){// 從第8位開始,包含第7位,取到結尾// 默認下標從0開始PingWeb = WebUrl.Substring(7);}else{// 從第7位開始,取index-7個字符PingWeb = WebUrl.Substring(7, index-7);}}// Ping網址bool PingResult = PingWebAddress(PingWeb);return PingResult;}#endregion#region 檢測與服務器的網絡連接狀態/// <summary>/// 檢測與服務器的網絡連接狀態/// </summary>/// <param name="WebUrl"></param>/// <returns></returns>public static bool PingWebAddress(string PingWebAddress){bool PinhRight = false;try{Ping Ping = new Ping();PingReply ReCode = Ping.Send(PingWebAddress);//Ping 成功!if (ReCode.Status == IPStatus.Success){PinhRight = true;}return PinhRight;}catch{return PinhRight;}}#endregion#region 檢測網站的狀態/// <summary>/// 檢測網站的狀態/// </summary>/// <param name="url">網站URL</param>/// <returns></returns>public static string CheckWebStatus(string WebUrl){// http的狀態碼,錯誤信息int httpcode = 000;string httpresult = null;try{WebRequest request = WebRequest.Create(WebUrl);request.Method = "HEAD";request.Timeout = 1000;HttpWebResponse response = (HttpWebResponse)request.GetResponse();//StatusCode為枚舉類型,200為正常//其他輸出為異常,需要轉為int型,才會輸出狀態碼httpcode = Convert.ToInt32(response.StatusCode);httpresult = response.StatusCode.ToString();response.Close();}catch (WebException ex){if (ex.Response != null){httpcode = Convert.ToInt32(((HttpWebResponse)ex.Response).StatusCode);httpresult = ((HttpWebResponse)ex.Response).StatusCode.ToString();}else{httpcode = 000;httpresult = "網站不存在!";}}return httpcode + httpresult;}#endregion#region 檢測網站的詳細信息/// <summary>/// 檢測網站的詳細信息/// </summary>/// <param name="errorCode">錯誤代碼</param>/// <returns></returns>public static string Checkhttpcode(string httpcode){// 錯誤詳情string httpdescript = "";int code = Convert.ToInt32(httpcode);// 判斷狀態碼switch (code){case 000:httpdescript = "網址不存在。";break;case 100:httpdescript = "繼續,請求者應當繼續提出請求。";break;case 101:httpdescript = "切換協議,請求者已要求服務器切換協議,服務器已確認并準備進行切換。";break;case 200:httpdescript = "成功,服務器已成功處理了請求。";break;case 201:httpdescript = "已創建,請求成功且服務器已創建了新的資源。";break;case 202:httpdescript = "已接受,服務器已接受了請求,但尚未對其進行處理。";break;case 203:httpdescript = "非授權信息,服務器已成功處理了請求,但返回了可能來自另一來源的信息。";break;case 204:httpdescript = "無內容,服務器成功處理了請求,但未返回任何內容。";break;case 205:httpdescript = "重置內容,服務器成功處理了請求,但未返回任何內容。";break;case 206:httpdescript = "部分內容,服務器成功處理了部分 GET 請求。";break;case 300:httpdescript = "多種選擇,服務器根據請求可執行多種操作。";break;case 301:httpdescript = "永久移動,請求的網頁已被永久移動到新位置。";break;case 302:httpdescript = "臨時移動,服務器目前正從不同位置的網頁響應請求,但請求者應繼續使用原有位置來進行以后的請求。";break;case 303:httpdescript = "查看其他位置,當請求者應對不同的位置進行單獨的 GET 請求以檢索響應時,服務器會返回此代碼。";break;case 304:httpdescript = "未修改,自從上次請求后,請求的網頁未被修改過。";break;case 305:httpdescript = "使用代理,請求者只能使用代理訪問請求的網頁。";break;case 307:httpdescript = "臨時重定向,服務器目前正從不同位置的網頁響應請求,但請求者應繼續使用原有位置來進行以后的請求。";break;case 400:httpdescript = "錯誤請求,服務器不理解請求的語法。";break;case 401:httpdescript = "身份驗證錯誤,此頁要求授權。";break;case 403:httpdescript = "禁止,服務器拒絕請求。";break;case 404:httpdescript = "未找到,服務器找不到請求的網頁。";break;case 405:httpdescript = "方法禁用,禁用請求中指定的方法。";break;case 406:httpdescript = "不接受,無法使用請求的內容特性響應請求的網頁。";break;case 407:httpdescript = "需要代理授權,指定請求者必須授權使用代理。";break;case 408:httpdescript = "請求超時,服務器等候請求時發生超時。";break;case 409:httpdescript = "沖突,服務器在完成請求時發生沖突。";break;case 410:httpdescript = "已刪除,請求的資源永久刪除后,服務器返回此響應。";break;case 411:httpdescript = "需要有效長度,服務器不接受不含有效內容長度標頭字段的請求。";break;case 412:httpdescript = "未滿足前提條件,服務器未滿足請求者在請求中設置的其中一個前提條件。";break;case 413:httpdescript = "請求實體過大,服務器無法處理請求,因為請求實體過大,超出服務器的處理能力。";break;case 414:httpdescript = "請求的 URI 過長,請求的 URI(通常為網址)過長,服務器無法處理。";break;case 415:httpdescript = "不支持的媒體類型,請求的格式不受請求頁面的支持。";break;case 416:httpdescript = "請求范圍不符合要求,如果頁面無法提供請求的范圍,則服務器會返回此狀態碼。";break;case 417:httpdescript = "未滿足期望值,服務器未滿足”期望”請求標頭字段的要求。";break;case 500:httpdescript = "服務器內部錯誤,服務器遇到錯誤,無法完成請求。";break;case 501:httpdescript = "尚未實施,服務器不具備完成請求的功能。";break;case 502:httpdescript = "錯誤網關,服務器作為網關或代理,從上游服務器收到無效響應。";break;case 503:httpdescript = "服務不可用,服務器目前無法使用(由于超載或停機維護)。";break;case 504:httpdescript = "網關超時,服務器作為網關或代理,但是沒有及時從上游服務器收到請求。";break;case 505:httpdescript = "HTTP 版本不受支持,服務器不支持請求中所用的 HTTP 協議版本。";break;default:httpdescript = "網頁不存在!";break;}return httpdescript;}#endregion} }檢測本地的網絡連接狀況,通過ping百度實現
檢測本地與服務器的網絡連接狀況,通過ping服務器實現
檢測服務器上網站的運行狀態,通過網站http協議的狀態碼和返回值實現,這里只能適用于http和https協議,如果,采用的其他協議,需要修改代碼
總結
以上是生活随笔為你收集整理的Ping命令检测网站运行状态的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASP.NET通过ajax调用后台方法
- 下一篇: Outlook邮箱重新配置