Example #1
0
        /// <summary>
        /// Pings list of websites for a response
        /// </summary>
        /// <returns></returns>
        public static async Task <List <LogOutput> > TestPing()
        {
            List <Task <PingReply> > requests = new List <Task <PingReply> >();

            //Queue links for pinging task
            foreach (PingTarget link in GlobalSettings.PingTests)
            {
                requests.Add(Task.Run(() =>
                {
                    var ping = new Ping();
                    return(ping.Send(link.Address));
                }));
            }

            List <LogOutput> resultsLog = new List <LogOutput>();
            LogOutput        output     = null;

            for (int i = 0; i < requests.Count; i++)
            {
                try
                {
                    PingTarget link = GlobalSettings.PingTests[i];
                    output         = new LogOutput();
                    output.Title   = link.Title;
                    output.Address = link.Address;

                    PingReply pingReply = await requests[i];

                    if (pingReply.Status == IPStatus.Success)
                    {
                        output.TestStatus = LogOutput.Status.Pass;
                        output.Details    = $"{Convert.ToString(pingReply.RoundtripTime)}ms";
                    }
                    else
                    {
                        output.TestStatus = LogOutput.Status.Fail;
                        output.Details    = pingReply.Status.ToString();
                    }


                    resultsLog.Add(output);
                }
                catch (Exception e)
                {
                    output.TestStatus = LogOutput.Status.Fail;
                    output.Details    = e.InnerException.Message.ToString();
                    resultsLog.Add(output);
                }
            }

            return(resultsLog);
        }
Example #2
0
        /// <summary>
        /// Checks to see if web sites will give a valid response
        /// </summary>
        /// <returns></returns>
        public static async Task <List <LogOutput> > TestWebResponse()
        {
            List <Task <string> > webrequests = new List <Task <string> >();

            foreach (WebTarget link in GlobalSettings.WebTests)
            {
                webrequests.Add(Task.Run(() =>
                {
                    WebClient wc = new WebClient();
                    return(wc.DownloadString(link.URL));
                }));
            }

            List <LogOutput> resultsLog = new List <LogOutput>();
            LogOutput        output     = null;

            for (int i = 0; i < webrequests.Count; i++)
            {
                try
                {
                    WebTarget link = GlobalSettings.WebTests[i];
                    output         = new LogOutput();
                    output.Title   = link.Title;
                    output.Address = link.URL;

                    string response = await webrequests[i];

                    output.TestStatus = LogOutput.Status.Pass;
                    output.Details    = "200";

                    resultsLog.Add(output);
                }
                catch (WebException we)
                {
                    output.Details = we.Message;

                    HttpWebResponse response = (HttpWebResponse)we.Response;
                    if (response != null)
                    {
                        if (response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.Forbidden)
                        {
                            output.TestStatus = LogOutput.Status.Pass;
                        }
                        else
                        {
                            output.TestStatus = LogOutput.Status.Fail;
                        }
                    }
                    else
                    {
                        output.TestStatus = LogOutput.Status.Fail;
                    }

                    resultsLog.Add(output);
                }
                catch (Exception e)
                {
                    output.TestStatus = LogOutput.Status.Fail;
                    output.Details    = Convert.ToString(e.Message);
                    resultsLog.Add(output);
                }
            }
            return(resultsLog);
        }