Skip to content

Instantly share code, notes, and snippets.

@jdfight
Created April 3, 2015 15:16
Show Gist options
  • Save jdfight/23add20bf5e64217bdf3 to your computer and use it in GitHub Desktop.
Save jdfight/23add20bf5e64217bdf3 to your computer and use it in GitHub Desktop.
Useful as a quick check to see if a client has an active internet connection.
//Adapted from http://answers.unity3d.com/questions/534873/testing-for-active-internet-connection.html
public static bool IsConnectedWeb(string checkurl = "http://www.google.com")
{
System.Net.WebClient client = null;
System.IO.Stream stream = null;
try
{
client = new System.Net.WebClient();
stream = client.OpenRead(checkurl);
if (client != null) { client.Dispose(); }
if (stream != null) { stream.Dispose(); }
return true;
}
catch (Exception ex)
{
if (client != null) { client.Dispose(); }
if (stream != null) { stream.Dispose(); }
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment