最近有位朋友說他有很多網址,可能有些已經過期或者不能訪問了。自己去一個一個點可以,但又很麻煩!
再過一段時間又要去檢查一次,每次都這樣就不方便了! 於是就做了個小程序給幫他檢測一下。
以下做了一個例子作為參考:
using System.Net; public bool CheckUrlVisit(string url) { try { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); if (resp.StatusCode == HttpStatusCode.OK) {
resp.Close(); return true; } } catch (WebException webex) { return false; } return false; } string[] links = { "http://www.baidu.com", "http://www.google.com", "http://www.chinabuffetflora.com" }; foreach (string link in links) { HttpContext.Current.Response.Write( link+" : "+CheckUrlVisit(link)+" <br>"); }
測試結果:
http://www.baidu.com : True
http://www.google.com : True
http://www.chinabuffetflora.com : False