Quantcast
Channel: Questions in topic: "httpwebrequest"
Viewing all articles
Browse latest Browse all 190

Internet Connection Check using 204 HTTPStatusCode

$
0
0
I've been working on an Internet Connection Checker... I've tried many things, from pinging to WebRequests, from RESTful APIs to socket beacons. The one thing I found most reliable and performatic, considering Unity restrictions, was to make a WebRequest to a 'no content' web page. Such as Google's 204 Generator. This will return a HttpWebResponse with the 'No Content' HttpStatusCode, which is labeled as number 204. This response has a length of 0 bytes, which means the only data that would be downloaded is the Header of the response. Not only that, but since the page will return a fixed status code, you can check if the user is trapped inside a Walled Garden. Although I didn't find any downsides on using this, I would like a second (and more) opinion on the topic. For instance, would this method be suited for constant streaming games? Such as MOBAs or real time multiplayer games? Is the overhead of a WebRequest plus the header from the WebResponse large enough for it to be not performatic data wise? Is this a reliable way to check for internet connection? Are there any problems worth mentioning? Have you guys ever tried this on a large scale project? Thank you! Ps.: Below is a simplified version of what I mean. public enum ConnectionStatus { ERROR, NO_DNS_CONNECTION, WALLED_GARDEN, CONNECTED } public static class ConnectionChecker { private static Uri google204 = new Uri("http://clients3.google.com/generate_204"); private static Thread checkerThread = null; private static bool threadIsRunning = false; private static bool threadShouldStop = false; public static ConnectionStatus Status { get; private set; } public static void StartConnectionCheck () { if (threadIsRunning && !threadShouldStop) return; if (checkerThread == null) checkerThread = new Thread (CheckConnection); checkerThread.Start (); } public static void StopConnectionCheck () { threadShouldStop = true; } private static void CheckConnection () { threadIsRunning = true; while (!threadShouldStop) { WebRequest request; HttpWebResponse response; try { request = WebRequest.Create (google204); response = (HttpWebResponse) request.GetResponse(); } catch (WebException webException) { Status = ConnectionStatus.NO_DNS_CONNECTION; continue; } catch (Exception e) { Status = ConnectionStatus.ERROR; continue; } if (response.StatusCode == HttpStatusCode.NoContent) Status = ConnectionStatus.CONNECTED; else Status = ConnectionStatus.WALLED_GARDEN; Thread.Sleep(1000); threadShouldStop = threadShouldStop || ThreadShouldStop(); } threadIsRunning = false; threadShouldStop = false; } private static bool ThreadShouldStop () { return Thread.CurrentThread.ThreadState == ThreadState.StopRequested || Thread.CurrentThread.ThreadState == ThreadState.SuspendRequested || Thread.CurrentThread.ThreadState == ThreadState.AbortRequested; } }

Viewing all articles
Browse latest Browse all 190

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>