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;
}

 

posted @ 2012-02-22 17:40  PPDev  阅读(5404)  评论(0编辑  收藏  举报