C# 判斷網站是否能訪問或者斷鏈


最近有位朋友說他有很多網址,可能有些已經過期或者不能訪問了。自己去一個一個點可以,但又很麻煩!

再過一段時間又要去檢查一次,每次都這樣就不方便了! 於是就做了個小程序給幫他檢測一下。

以下做了一個例子作為參考:

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 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM