Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1.  
  2. public static string GetHtmlFromUri(string resource)
  3. {
  4. string html = string.Empty;
  5. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(resource);
  6. try
  7. {
  8. using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
  9. {
  10. bool isSuccess = (int)resp.StatusCode < 299 && (int)resp.StatusCode >= 200;
  11. if (isSuccess)
  12. {
  13. using (StreamReader reader = new StreamReader(resp.GetResponseStream()))
  14. {
  15. //We are limiting the array to 80 so we don't have
  16. //to parse the entire html document feel free to
  17. //adjust (probably stay under 300)
  18. char[] cs = new char[80];
  19. reader.Read(cs, 0, cs.Length);
  20. foreach (char ch in cs)
  21. {
  22. html += ch;
  23. }
  24. }
  25. }
  26. }
  27. }
  28. catch
  29. {
  30. return "";
  31. }
  32. return html;
  33. }
  34.  
  35. public static bool CheckConnection()
  36. {
  37. string HtmlText = GetHtmlFromUri("http://google.com");
  38. if (HtmlText == "")
  39. {
  40. //No connection
  41. Debug.Log("No Internet Connection");
  42. return false;
  43.  
  44. }
  45. else if (!HtmlText.Contains("schema.org/WebPage"))
  46. {
  47. //Redirecting since the beginning of googles html contains that
  48. //phrase and it was not found
  49. return false;
  50.  
  51. }
  52. else
  53. {
  54. //success
  55. // Debug.Log("Internet access");
  56. return true;
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement