Esempio n. 1
0
        public static Tuple <DateTime, DateTime, int, string, int, string> GetWebAppTime(int scenario)
        {
            int    httpStatusCode = 0;
            string dataString     = "";
            string url            = "";
            string statusString   = "";
            int    status         = 0;

            //Forced to assign a value.
            DateTime startTimeUTC = DateTime.UtcNow;
            DateTime endTimeUTC   = DateTime.UtcNow;


            WebClient webClient = new WebClient();
            //webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

            //The Local host URL below should be static. However, if you have issues for
            //use the instructions below:
            //Open WebApplication.sln in VS. Select the Solution Explorer tab and open Properties.
            //Select Web from the left tab and update the localhost with your Project URL below

            string localHost = "https://localhost:44342/";


            switch (scenario)
            {
            case (1):
                status = 1;
                url    = localHost + "HttpHandler.aspx";

                HttpWebRequest  myHttpWebRequest1  = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse myHttpWebResponse1 = (HttpWebResponse)myHttpWebRequest1.GetResponse();
                statusString   = myHttpWebResponse1.StatusDescription;
                httpStatusCode = (int)myHttpWebResponse1.StatusCode;
                endTimeUTC     = DateTime.UtcNow;
                dataString     = webClient.DownloadString(url);
                startTimeUTC   = DateTime.Parse(dataString);
                SQLInsert(startTimeUTC, endTimeUTC, httpStatusCode, dataString, status, statusString);
                break;

            case (2):
                status = 2;
                url    = localHost + "HttpHandler.aspx?simulate500=Yes";

                try
                {
                    HttpWebRequest  myHttpWebRequest  = (HttpWebRequest)WebRequest.Create(url);
                    HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
                }
                catch
                {
                    //Considered adding better error handling, but it appears that it will error out regardless, whether at console app level or database level.
                    //In a normal situation, I'd pursue a better option to capture this. However, due to the dates' not null requirments of the DB,
                    //it cannot allow capturing this activity even in a partial sense.
                    status         = 2;
                    httpStatusCode = 500;
                    statusString   = "Simulated 500 Error";
                    dataString     = "Task failed succesfully";
                    Console.WriteLine(httpStatusCode.ToString() + " - " + statusString + " - " + dataString + " - Status: " + status);
                }
                break;

            case (3):
                try
                {
                    url = localHost + "HttpHandler.aspx";

                    HttpWebRequest myHttpWebRequest2 = (HttpWebRequest)WebRequest.Create(url);

                    //Set timeout to 1ms to ensure that a timeout is guaranteed.
                    myHttpWebRequest2.Timeout = 1;
                    HttpWebResponse myHttpWebResponse2 = (HttpWebResponse)myHttpWebRequest2.GetResponse();
                    Console.WriteLine("Timeout failed");
                }
                catch
                {
                    httpStatusCode = 408;
                    status         = 2;
                    statusString   = "Timeout";
                    dataString     = "Task failed succesfully";
                    Console.WriteLine(httpStatusCode.ToString() + " - " + statusString + " - " + dataString + " - Status: " + status);
                }
                break;

            default:
                Console.WriteLine("A scenario of 1-3 was not selected");
                break;
            }

            var timeStatus = Tuple.Create(startTimeUTC, endTimeUTC, httpStatusCode, dataString, status, statusString);

            return(timeStatus);
        }