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

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

问题描述

C# 2008 SP1

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.

所以,我想要做的是使用 httpWebRequest 类连接到我们的客户网站.如果响应正常,则可以继续 Internet 连接.

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.

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

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

所以要求是:

有时会在客户办公室使用代理.我不能使用 TCPClient 类(没有代理属性).

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).

代理不支持 SOCKS,因此不能使用 Sockets 类.

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

我需要使用超时属性.所以不能使用 WebClient 类.这是因为软电话会冻结,直到返回响应.所以几秒钟后超时.

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.

所以我唯一能想到的就是 HttpWebRequest 类.

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;
            }

======== 编辑使用 pInvoke ====

======== Edit using pInvoke ====

 [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);

推荐答案

尝试使用 P/Invoke 调用 InternetGetConnectedState.这应该告诉您是否配置了连接.然后,您可以尝试使用 InternetCheckConnection<检查与您的服务的特定连接/a>.这(有时)比直接连接连接要快,但我会对其进行测试,看看它是否比预先进行完整连接更好.

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天全站免登陆