判断是否可以连接服务器?
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;
}
{
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;
}