What is the best way to discover your internet connection using .NET?

I have a Windows Forms application. When a user imports a license for my application, I would like to “call home” (click on the aspx page on the Internet) and register the license information.

The problem is that the user may not have an Internet connection working at that moment. I would like to check it out first. What is the best way to determine if a user has access to the Internet?

+4
source share
7 answers

At least one hundred thousand dollars were spent on this issue at the PPOE. The main points of attachment:

  • For a long time, an HTTP connection will exit and return with a “200 OK” message. Only this is not your site; this is a hotel / cafe / everything the router offers you.

  • The APIs will lie in interesting and creative ways. For example, some Windows network APIs return bad connection data if you used a modem, but then upgraded to DSL but did not start the wizard.

  • In some (very important) places trying to connect, a connection will begin - on a dial-up telephone - with a charge per minute. Again, in PPOE they sent a fairly popular bit of software that did it at 3:00 in the morning. Made for some seriously grumpy customers.

  • Some major Internet service providers have "unique" software when working on a broadband network, where they are not smart enough to just use the nice Ethernet and TCP / IP that they are provided with. Instead, they use Ethernet to emulate a modem (which, in turn, is configured to emulate an Ethernet + tcp / ip installation). It's called PPPOE, and it's hard to tell which adapter you're trying to use.

  • More and more houses use DSL and Cabel modems, where the computer is “connected”, but only in the local field; the local unit may have lost contact and you will never know.

DECISION ONLY - try to connect and confirm the return.

+11
source

Check the WebRequest.GetResponse Method

// Create a new WebRequest Object to the mentioned URL. WebRequest myWebRequest=WebRequest.Create("http://www.contoso.com"); Console.WriteLine("\nThe Timeout time of the request before setting is : {0} milliseconds",myWebRequest.Timeout); // Set the 'Timeout' property in Milliseconds. myWebRequest.Timeout=10000; // This request will throw a WebException if it reaches the timeout limit before it is able to fetch the resource. WebResponse myWebResponse=myWebRequest.GetResponse(); 

or you can call the InternetGetConnectedState API

 [DllImport("wininet.dll")] private extern static bool InternetGetConnectedState(out int Description, int ReservedValue ) ; public static bool IsConnectedToInternet() { int Desc ; return InternetGetConnectedState(out Desc, 0); } 
+8
source

I do not see any advantage in testing if the connection is available before executing the request. Once you have checked, the connection may still disappear, and there is no reliable way to tell if the request will work before you try it. I also do not see any benefits to the user.

The only legitimate test I would see is if there are any network interfaces available to avoid an anonymous user with a remote access hint if he / she is still in remote access mode.

+3
source

You can send ping to the "home" host to see if it returns a response.

Net :: Ping

+1
source

Define "internet connection." For me there is no "internet connection". There are only tcp / ip connections, DNS servers and web servers, etc. The best you can do is try to load the webpage and see if it works. As suggested, you can try ping yourself, but this does not mean that the web server is working, and that the page can be loaded. You can be sure of this only when loading the page.

+1
source

One of the methods I've used is to create a web service with a boolean method that always returns true. Call it from your application, and if you get any exceptions, or "false", you are not connected.

  [WebMethod] public bool IsAvailable() { return true; } 

and name it:

  private bool IsOnline() { Cursor = Cursors.WaitCursor; var proxy = new WS(); bool IsOnline = false; try { IsOnline = proxy.IsAvailable(); } catch (System.Net.WebException WebEx) { NameValueCollection nvc = new NameValueCollection(); nvc.Add("WebException Status", WebEx.Status.ToString()); if (WebEx.Status == System.Net.WebExceptionStatus.ProtocolError) { nvc.Add("Status Code", ((System.Net.HttpWebResponse)WebEx.Response).StatusCode.ToString()); nvc.Add("Status Description", ((System.Net.HttpWebResponse)WebEx.Response).StatusDescription); } ExceptionManager.Publish(WebEx, nvc); } catch (Exception) { IsOnline = false; } Cursor = Cursors.Default; return IsOnline; } 
+1
source

When I always needed to check the Internet connection in my application, I would simply ping google.com, since it never went down the seams.

0
source

All Articles