Skip to content

Instantly share code, notes, and snippets.

@tim-hub
Last active July 1, 2016 09:01
Show Gist options
  • Save tim-hub/6fd4c11b32682d1622841dc52d3391fb to your computer and use it in GitHub Desktop.
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)
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;
}
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