测试互联网连接速度最快的方法 [英] Fastest way to test internet connection

查看:207
本文介绍了测试互联网连接速度最快的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#2008 SP1

我用这code连接到我们的客户的网站。这为软电话应用。该用户打电话之前,网络电话有来测试是否有有效的Internet连接。

所以,要我做的是使用HttpWebRequest类连接到客户的网站。如果响应是确定的,那么互联网连接可以继续进行。

不过,我已经注意到,响应时间太长作出回应。我不知道这是不是测试一个非常有效的方式。

然而,当我浏览他们的网站,它需要不到一秒钟的加载页面。但是,当我使用HttpWebRequest类花费的时间太长

因此​​,对于这个要求是:

有时代理将在客户的办公室使用。要我不能使用的TcpClient类(不具有代理的属性)。

代理服务器不支持SOCKS所以不能使用套接字类。

我需要使用一个超时属性。所以不能使用WebClient类。这是因为在软电话将冻结,直到返回一个响应。几秒钟后,所以配置超时。

所以,我唯一能想到的是HttpWebRequest类。

  HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(http://www.xxxxxxxxx.com);
            request.Timeout = 5000;
            request.Credentials = CredentialCache.DefaultNetworkCredentials;
            HttpWebResponse响应=(HttpWebResponse)request.GetResponse();            如果(response.Status code ==的HTTPStatus code.OK)
            {
                Console.WriteLine(IsSIPServerAvailable:+ response.Status code);
                isAvailable = TRUE;
            }

========用对\\调用编辑====

 函数[DllImport(wininet.dll文件,字符集= CharSet.Auto)
    私有外部静态布尔的InternetGetConnectedState(REF InternetConnectionState_e lpdwFlags,诠释dwReserved); [国旗]
    枚举InternetConnectionState_e:INT
    {
        INTERNET_CONNECTION_MODEM =为0x1,
        INTERNET_CONNECTION_LAN = 0X2,
        INTERNET_CONNECTION_PROXY =为0x4,
        INTERNET_RAS_INSTALLED = 0×10,
        INTERNET_CONNECTION_OFFLINE = 0x20的,
        INTERNET_CONNECTION_CONFIGURED = 0X40
    } //在用于检查网络功能
 InternetConnectionState_e标志= 0;
 BOOL isConnected =的InternetGetConnectedState(REF标志,0);


解决方案

尝试使用P / Invoke调用<一个href=\"http://msdn.microsoft.com/en-us/library/aa384702%28VS.85%29.aspx\">InternetGetConnectedState.这应该告诉你,你是否拥有配置的连接。然后,您可以尝试检查使用 InternetCheckConnection 您服务特定连接。这就是(有时)速度比直接挂钩的联系,但我会测试它,看它是否是任何不仅仅是做一个完整的连接前面好。

C# 2008 SP1

I am using this code to connect to our client website. This is for a softphone application. Before the user makes a call, the softphone has to test if there is an active Internet connection.

So, want I have done is used the httpWebRequest class to connect to our clients website. If the response is ok, then the Internet connection can proceed.

However, I have noticed that the response is taking too long to respond. I am not sure if this is not a very efficient way to test.

However, when I browse to their website, it takes less than a second to load the page. But takes too long when I use the HttpWebRequest class

So requirements for this are:

Sometime a proxy will be used at the client's office. To I can't use the TCPClient class (doesn't have a proxy property).

The proxy doesn't support SOCKS so cannot use the Sockets class.

I need to use a timeout property. So cannot use the WebClient class. This is because the softphone would freeze until a response is returned. So timeout after a few seconds.

So the only one I can think of is the HttpWebRequest class.

  HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.xxxxxxxxx.com");
            request.Timeout = 5000;
            request.Credentials = CredentialCache.DefaultNetworkCredentials;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            if (response.StatusCode == HttpStatusCode.OK)
            {
                Console.WriteLine("IsSIPServerAvailable: " + response.StatusCode);
                isAvailable = true;
            }

======== Edit using p\Invoke ====

 [DllImport("wininet.dll", CharSet = CharSet.Auto)]
    private extern static bool InternetGetConnectedState(ref InternetConnectionState_e lpdwFlags, int dwReserved);

 [Flags]
    enum InternetConnectionState_e : int
    {
        INTERNET_CONNECTION_MODEM = 0x1,
        INTERNET_CONNECTION_LAN = 0x2,
        INTERNET_CONNECTION_PROXY = 0x4,
        INTERNET_RAS_INSTALLED = 0x10,
        INTERNET_CONNECTION_OFFLINE = 0x20,
        INTERNET_CONNECTION_CONFIGURED = 0x40
    }

 // In function for checking internet
 InternetConnectionState_e flags = 0;          
 bool isConnected = InternetGetConnectedState(ref flags, 0);

解决方案

Try using P/Invoke to call InternetGetConnectedState. That should tell you whether or not you have a connection configured. You can then try checking the specific connection to your service using InternetCheckConnection. This is (sometimes) quicker than hooking up the connection directly, but I'd test it to see if it's any better than just doing a full connection up front.

这篇关于测试互联网连接速度最快的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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