日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Ping命令检测网站运行状态

發布時間:2025/3/20 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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命令检测网站运行状态的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产精品久久综合视频 | 污污视频免费观看 | 亚洲人人精品 | 自拍视频在线 | 欧洲av片 | www.chengren| 日韩色av | 美女扣逼喷水视频 | 国产精品xxxxx | 国产亚洲色婷婷久久99精品91 | 久久久久久国产 | 亚洲一区二区三区影视 | 国产精品一级二级 | 一区小视频 | 一区免费在线 | 国产精品无码电影在线观看 | 极品91尤物被啪到呻吟喷水 | 美女被娇喘流出白 | 国产午夜精品久久久 | 欧美激情成人网 | 亚洲精品久久久蜜桃 | 午夜精品福利在线 | 欧美第1页 | 偷拍视频一区 | 国产成人三级一区二区在线观看一 | 国产www精品 | 亚洲精品国产精品国自产 | 一区二区三区中文视频 | 精品成人av| 免费a网址 | 国产精品无码网站 | 日本一区二区精品视频 | 黄色羞羞网站 | 久久精品一 | 91.xxx.高清在线 | 国产成人精品久久二区二区 | 麻豆精品国产传媒 | 一级性爱视频 | 日本xxxxxxxxx| 能在线观看的av | 精品视频99| 极品少妇xxxx| 91精品999| 成人黄色电影网址 | 亚洲骚片 | 伊人激情| 久久久久久久极品 | 中文天堂在线资源 | 人妻无码一区二区三区久久 | 成人免费毛片网站 | 久久综合久久88 | 中文字幕在线观看你懂的 | 欧美黄色aaa| 性视频播放免费视频 | 天堂av一区 | 制服丝袜亚洲色图 | 咪咪色在线视频 | 久操福利| 91精品婷婷国产综合久久蝌蚪 | 成年人免费观看视频网站 | 国产精品网友自拍 | 欧美顶级metart裸体全部自慰 | 亚洲欧洲国产视频 | 欧美日本韩国一区二区三区 | 欧美综合在线视频 | 国产性爱精品视频 | 蝌蚪自拍网站 | 中文字幕在线观看三区 | 国产高清在线观看 | 情五月| 亚洲一级片免费 | 日本一区二区三区在线免费观看 | 黄wwwww | 国模吧无码一区二区三区 | 丰满放荡岳乱妇91ww | 久久久久网站 | 蜜臀久久精品久久久用户群体 | 日韩精品在线一区二区 | 欧洲美女与动交zozzo | 国产这里有精品 | 国产精品影院在线观看 | 视频一区 国产 | 成人午夜av在线 | 美日韩免费视频 | 99久久综合国产精品二区 | 99热香蕉 | 欧美一级特黄aaaaaa | 国产av一区二区三区最新精品 | 国产一区免费在线观看 | 久久久艹 | 在线观看黄色免费网站 | 婷婷激情网站 | 无码人妻精品一区二区三区99v | 激情五月开心婷婷 | 成人性生交大片免费卡看 | 亚洲高清不卡 | 日韩欧美一区二区三区久久婷婷 | 免费在线观看黄视频 | 成人av影视在线观看 |