.net – What’s the best way to detect an internet connection using .NET


I have a Windows Forms application. When the user imports a license for my application I'd like to "phone home" (hit an aspx page on the web) and register the license information.

The problem is that the user may not have an internet connection working at that moment. I'd like to test this first. What's the best way to detect if the user has an internet connection available?

Best Solution

At a PPOE, at least a hundred thousand dollars was spent on this question. The main sticking points:

  1. A lot of the time, an HTTP connection will go out, and come back with a "200 OK" message. Only it's not your site; it the hotel/cafe/whatever router offering to connect you.

  2. The APIs will lie in interesting and creative ways. For example, some of the Windows network APIs return bad connection data if you used to have a modem, but then upgraded to DSL, but didn't run the wizard.

  3. In some (very important) places, trying to connect will start a connection -- on dialup -- with a per-minute charge. Again, at the PPOE they shipped a reasonably popular bit of software that did this at 3:00 AM. Made for some seriously grumpy customers.

  4. Some major ISPs have "unique" software when run on broadband where they weren't clever enough to just use the nice Ethernet and TCP/IP they are given. Instead they use the Ethernet to emulate a modem (which in turn is set up to emulate an Ethernet +tcp/ip setup). It's called PPPOE and it makes it hard to tell what adapter you are trying to use

  5. More and more homes are using DSL and Cabel modems where the computer is "connected", but only to the local box; the local box may have lost connectivity and you won't ever know.

THE ONLY SOLUTION is to try a connection AND validate the return.