日韩性视频-久久久蜜桃-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无码一区二区三区 | 调教一区二区三区 | 久久中文字幕一区 | 久久久噜噜噜久久久 | 一区二区三区精品视频 | 尤物193.com | 91在线公开视频 | たちの熟人妻av一区二区 | 成人久久精品人妻一区二区三区 | 亚洲综合在线中文字幕 | 水蜜桃av无码 | 美日韩av在线 | 久久九九久久九九 | 九色视频国产 | 国产女人叫床高潮大片免费 | 欧美色涩 | 亚洲精品天堂成人片av在线播放 | 我的丝袜美腿尤物麻麻 | 校园春色自拍偷拍 | 天天摸天天操 | 久久91精品国产 | ass精品国模裸体欣赏pics | 欧美成人精品激情在线视频 | 破处视频在线观看 | 高清国产一区二区 | 国产区精品在线观看 | 国产一区二区在线视频观看 | 欧美在线观看一区二区三区 | 国产成人精品一区二 | 国产美女裸体无遮挡免费视频 | 国产字幕侵犯亲女 | 亚洲精品一区二三区不卡 | 亚洲一区二区三区四区在线 | 亚洲aⅴ在线 | 丝袜美女啪啪 | 亚洲国产精品女人久久久 | 欧美综合国产 | 久久久久亚洲av无码专区体验 | 永久免费av | 极品少妇一区二区三区 | 永久免费视频网站直接看 | 老太婆av | 欧美丰满一区二区免费视频 | www.com亚洲 | 久久久久久久999 | 男生插女生视频 | 日韩一区二区高清 | 国产成人无码av在线播放dvd | 国产精品第二页 | 伊人日日夜夜 | 精品人妻一区二区三区久久夜夜嗨 | 亚洲喷水 | 欧美一级全黄 | 午夜激情在线观看视频 | 91精品国产乱码久久 | 在线观看的av网址 | 黄a在线观看 | 亚洲综合第一 | 免费视频爱爱太爽 | 免费av看片 | 夜夜操免费视频 | 九九九九九热 | 欧美日韩天堂 | 四虎国产精品成人免费入口 | 国产欧美精品一区二区色综合朱莉 | 亚洲乱乱| 日韩中文字幕亚洲 | 男人插女人网站 | 福利视频不卡 | 国产丰满农村老妇女乱 | 国产男女猛烈无遮挡免费观看网站 | 在线观看日本一区二区 | 天天干妹子 | 日韩欧美一区二区区 | 国产毛毛片| 精品福利一区 | 桃色一区 | 亚洲黄片一区二区 | 日韩一区二区三区久久 | 手机在线免费看av | 国产91区 | 中文字幕在线播出 | 免费插插视频 | 91精品国产综合久久香蕉922 | 一区二区乱码 | 老湿福利影院 | 日韩中文字幕av电影 | 狠狠操天天操夜夜操 | 一区二区三区日韩精品 | 欧美三个黑人玩3p | 亚洲天堂手机在线观看 | 日日夜夜91 | 久久久久久久久影院 | 夜夜操天天射 | 日韩av片在线看 | 香蕉a| 国产野外作爱视频播放 | 精品人妻一区二区三区日产 | 午夜插插插 |