测试Internet访问
问题描述:
有多种方法可以测试互联网连接。
例如:
Hi,
There are many ways to test connection of internet .
for example :
public bool WebRequestTest()
{
string url = "http://www.google.com";
try
{
System.Net.WebRequest myRequest = System.Net.WebRequest.Create(url);
System.Net.WebResponse myResponse = myRequest.GetResponse();
return true;
}
catch (System.Net.WebException)
{
return false;
}
}
但我无法找到测试互联网接入的方法!当Internet Access不可用时,此方法返回TRUE !!!
您有什么方法(方法)来测试Internet访问吗?@
非常感谢,,,
but I cant find a way for test Internet Access ! When Internet Access is not available this method return TRUE !!!
do you have any way (method) to test Internet Access ?@
many thanks ,,,
答
你不检查响应是否有效。
Debug一次连接时的响应和未连接时的一次响应。您将看到差异。
You do not check whether the response is valid.
Debug the response one time you are connected and one time while not connected. You will see then the difference.
请参阅CodeProject文章:如何使用Windows NLM API获得新网络连接的通知[ ^ ]。
但是,请记住,随着.NET FrameWork的发展,其访问网络/互联网连接状态的设施也发生了变化:[ ^ ]。
您应该评估Win> = 8 [ ^ ]。
此外,请考虑Eric Lippert的评论:另外,请注意,无法检查用户是否已连接到互联网;您可以告诉他们过去是否已连接。假设您有方法:bool c =已连接(); if(c){DoSomething(); - 在调用IsConnected和DoSomething之间,无线网络路由器可能已被拔掉.IsConnected确实应该被称为WasRecentlyConnected。 - Eric Lippert 2010年1月9日15:47......在StackOverFlow线程上:[ ^ ]。
See CodeProject article: "How to use the Windows NLM API to get notified of new network connectivity" [^].
But, keep in mind that as the .NET FrameWork has evolved, so has its facilities for access to the state of network/internet-connections changed: [^].
You should evaluate any possible changes, or other methods, in Win >= 8 [^].
Also, consider Eric Lippert's comment: "Also, note that there is no way to check if the user is connected to the internet; all you can tell is if they were connected in the past. Suppose you had a method: "bool c = IsConnected(); if (c) { DoSomething(); } " -- between the call to IsConnected and DoSomething, the wireless network router might have been unplugged. IsConnected really should be called WasRecentlyConnected. – Eric Lippert Jan 9 '10 at 15:47" ... on a StackOverFlow thread: [^].