From 416c17ecadb6fe9b4875cdf075ac5482be9e9084 Mon Sep 17 00:00:00 2001 From: Miepee Date: Thu, 21 Jul 2022 00:22:18 +0200 Subject: [PATCH] Optimize IsConnectedToInternet Previous implementation visited the site, now it only pings. --- AM2RLauncher/AM2RLauncherLib/HelperMethods.cs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/AM2RLauncher/AM2RLauncherLib/HelperMethods.cs b/AM2RLauncher/AM2RLauncherLib/HelperMethods.cs index 013af9c..dd96a5b 100644 --- a/AM2RLauncher/AM2RLauncherLib/HelperMethods.cs +++ b/AM2RLauncher/AM2RLauncherLib/HelperMethods.cs @@ -2,6 +2,7 @@ using System; using System.IO; using System.Net; +using System.Net.NetworkInformation; using System.Security.Cryptography; namespace AM2RLauncherLib; @@ -141,18 +142,14 @@ public static class HelperMethods } /// - /// Checks if we currently have an internet connection, by pinging github. + /// Checks if we currently have an internet connection, by pinging GitHub. /// /// if we have internet, if not. public static bool IsConnectedToInternet() { log.Info("Checking internet connection..."); - HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://github.com"); - try - { - request.GetResponse(); - } - catch (WebException) + PingReply pingReply = new Ping().Send("https://github.com"); + if (pingReply?.Status != IPStatus.Success) { log.Info("Internet connection failed."); return false;