Last active
July 1, 2016 09:01
-
-
Save tim-hub/6fd4c11b32682d1622841dc52d3391fb to your computer and use it in GitHub Desktop.
check internet [How to 100% check internet availability](http://answers.unity3d.com/questions/567497/how-to-100-check-internet-availability.html)
This file contains hidden or 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
using System.Net; | |
public string GetHtmlFromUri(string resource) | |
{ | |
string html = string.Empty; | |
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(resource); | |
try | |
{ | |
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse()) | |
{ | |
bool isSuccess = (int)resp.StatusCode < 299 && (int)resp.StatusCode >= 200; | |
if (isSuccess) | |
{ | |
using (StreamReader reader = new StreamReader(resp.GetResponseStream())) | |
{ | |
//We are limiting the array to 80 so we don't have | |
//to parse the entire html document feel free to | |
//adjust (probably stay under 300) | |
char[] cs = new char[80]; | |
reader.Read(cs, 0, cs.Length); | |
foreach(char ch in cs) | |
{ | |
html +=ch; | |
} | |
} | |
} | |
} | |
} | |
catch | |
{ | |
return ""; | |
} | |
return html; | |
} |
This file contains hidden or 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
using System.Net; | |
void Start() | |
{ | |
string HtmlText = GetHtmlFromUri("http://google.com"); | |
if(HtmlText == "") | |
{ | |
//No connection | |
} | |
else if(!HtmlText.Contains("schema.org/WebPage")) | |
{ | |
//Redirecting since the beginning of googles html contains that | |
//phrase and it was not found | |
} | |
else | |
{ | |
//success | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment