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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM