C#判斷連接是否可下載

private static bool IsCanConnect(string url)
{
            HttpWebRequest req = null;
            HttpWebResponse res = null;
            bool CanCn = true;   //設成可以連接; 
            try
            {
                req = (HttpWebRequest)WebRequest.Create(url);
                res = (HttpWebResponse)req.GetResponse();
            }
            catch (Exception)
            {
                CanCn = false;   //無法連接 
            }
            finally
            {
                if (res != null)
                {
                    res.Close();
                }
            }
            return CanCn;
}

原文章地址:http://www.cnblogs.com/xiaopin/archive/2012/02/22/2363519.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章