测试Internet访问 [英] Test Internet Access

查看:72
本文介绍了测试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线程上:[< a href =http://stackoverflow.com/q/2031824target =_ blanktitle =新窗口> ^ ]。
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: [^].


这篇关于测试Internet访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆