您的位置:首页 > 编程语言 > C#

C# 判断是否可以连接服务器?

2012-02-22 17:40 483 查看
判断是否可以连接服务器?

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: