ICMP禁用/阻止:如何使用C#检查Internet连接
答
我还没有尝试过,但我认为以下工作。
HttpWebRequest req;
HttpWebResponse resp;
req = (HttpWebRequest)WebRequest.Create("http://www.stackoverflow.com");
resp = (HttpWebResponse)req.GetResponse();
if(resp.StatusCode.ToString().Equals("OK"))
{
Console.WriteLine("its connected.");
}
else
{
Console.WriteLine("its not connected.");
}
我认为这将做的工作,而且可能需要添加“System.Net”左右的usings。
我不得不补充,我认为创建一个简单的页面,如google.com,或甚至更简单的'example.com'的响应可能会提供更快的结果。但是,我不确定GetResponse是否已经将所有内容加载到内存中,或者只是响应检查以查看它是否可以访问。 – Honnes 2011-04-01 05:48:30
我刚刚发现了另一种方法,但它使用一些DLL来检查Internet连接,可以在以下网址找到解决方案:http://www.dotnetspider.com/forum/264358-How-check-whether-internet-connection-exists。 aspx – Honnes 2011-04-01 05:54:47
而不是url,请使用IP地址来检查互联网连接。 – Vinoth 2011-04-03 06:59:00