62,046
社区成员
发帖
与我相关
我的任务
分享
public static bool IsWebAccessible()
{
HttpWebRequest request;
HttpWebResponse response;
string Url = Config.Config.AppConfig[ "TestUrl "].ToString();
bool bConnected = false;
try
{
request = (HttpWebRequest)WebRequest.Create(Url);
response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
bConnected = true;
}
response.Close();
}
catch (WebException we)
{
bConnected = false;
EventLog.Write(we);
EventLog.Write(new Exception( "无法连接到服务器: " +Url));
}
catch (Exception ex)
{
bConnected = false;
EventLog.Write(ex);
}
finally
{
request = null;
response = null;
}
return bConnected;
}
public bool Ping(string ip)
{
System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions();
options.DontFragment = true;
string data = "L";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 1000; // Timeout 时间,单位:毫秒
System.Net.NetworkInformation.PingReply reply = p.Send(ip, timeout, buffer, options);
if (reply.Status == System.Net.NetworkInformation.IPStatus.Success)
return true;
else
return false;
}
Microsoft.VisualBasic.Devices.Computer com = new Microsoft.VisualBasic.Devices.Computer();
bool result = com.Network.Ping(TextBox1.Text);
Response.Write("ping " + TextBox1.Text + "结果:" + result.ToString());