Created
April 3, 2015 15:16
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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