Check internet connection programmatically – Internet Verbindung programmatisch prüfen

I ran into a project where I was asked to create an application that would switch the proxy settings depending on wether an nternet connection exists or not. The problem was that this should also work if the user is working from a hotel room where he is logging into a pay account on a mobile lan spot. Therefore I had to check if the connection was open (payed) or closed (not yet payed but nontheless connected to the internet).

———————————————————–

In einem mener Projekte war gefordert eine Applikation zu liefern, welche die Proxy Einstellungen des Browsers verändert in Abhängigkeit davon ob der Computer mit dem Internet verbunden ist, oder nicht. Dies sollte auch dann funktionieren, wenn sich der Benutzer in einem Hotel an einem mobilen LAN Spot anmeldet (kostenpflichtig). Also musste ich prüfen, ob eine freie Verbindung besteht (bezahlt) oder sich der Benutzer gerade an dem LAN Spot anmeldet (noch nicht bezahlt, aber trotzdem eine bestehende Internet-Verbindung).
Solution:

Check for an open connection by using a HTTP-Webrequest to a website. I chose to use google.com because this site is most likely always online.

using System.Net;
using System.Net.Cache;

public static bool CheckConnection() 
{
            RequestCachePolicy policy = new RequestCachePolicy(RequestCacheLevel.Revalidate);
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(„http://www.google.com/„);
            WebResponse objWebResponse;
            objWebResponse = myReq.GetResponse();
            if (objWebResponse.IsFromCache==true)
            {
                myReq.CachePolicy = policy;
                clearIECache();
                objWebResponse = myReq.GetResponse();
                if (objWebResponse.ContentLength > 1)
                {
                    return true;
                }
            }
            if (objWebResponse.ContentLength > 1)
            {
               return true;
            }
            return false;
}

This function returns true if a HTTP-Connection was established or false if no connection could be established.
The fist line created a new request policy which makes sure that the request is sent to the server and not taken from the browsers cache.
Just in case I’m validating this in the line „if (objWebResponse.IsFromCache==true)“ and do a clearing of the internet cache with a separate function ClearIECache().
I got this from another website.

public static void clearIECache()
{
            ClearFolder(new DirectoryInfo(Environment.GetFolderPath
               (Environment.SpecialFolder.InternetCache)));
}

public static void ClearFolder(DirectoryInfo folder)
{
            foreach (FileInfo file in folder.GetFiles())
            { file.Delete(); }
            foreach (DirectoryInfo subfolder in folder.GetDirectories())
            { ClearFolder(subfolder); }
}

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert