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)
            {
                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 

posted @ 2012-10-30 11:39  沙耶  阅读(278)  评论(0编辑  收藏  举报