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);
The quickest way to run an accurate, consistent and reliable speed test is to go to Speedtest.net and download the designated app for the device you want to test. Speedtest.net is a free service run by Ookla that has native apps for pretty much any platform, including iOS, Android, Windows and macOS.
Speedtest.net is probably the most well-known speed test. It's fast, free, and has available to it a huge list of worldwide test locations, making for more accurate results than average.
Speed tests are a great way to ensure you are getting the right amount of bandwidth through your connection. However, many experts have found that speed tests are too often inaccurate.
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.
What is the softphone going to use for its real communication? Is that going over HTTP/HTTPS to the same web site? If so, that's absolutely the right way to go - the next step is to work out why it's taking so long.
Is the network connection definitely up before you may the request? Are you definitely not making any requests beforehand? I ask because I notice you're not disposing of the response - if that happens elsewhere as well, you may find that you're running up against the connection pool only giving you a couple of connections to a particular server. The moral is to always put HttpWebResponse
s in a using
statement. Of course, that may well not be the problem in your case, but it's worth a try.
If your actual application is going to connect elsewhere, then that's where your test should check as well. Basically, make it as close to the real thing as possible.
Out of interest, you say it's a "softphone" application - is this actually running on a phone of some description, using the compact framework, or is it a desktop app?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With