Example #1
0
        /// <summary>
        /// Executes an HTTP GET request against the Url specified, returning the 
        /// entire response body in string form.
        /// </summary>
        /// <param name="url">The URL to request</param>
        /// <param name="requireHttp200">
        /// Boolean indicating whether or not to return 
        /// null if the repsonse status code is not 200 (OK).
        /// </param>
        /// <returns>
        /// The response body or null if the response status is required to 
        /// be 200 (OK) but is not
        /// </returns>
        internal static string PerformHttpGet(string url, bool requireHttp200) {
            var restClient = new RestClient();
            var response = restClient.Get(url);

            if (requireHttp200 || response.StatusCode != HttpStatusCode.OK)
                return null;

            return response.Body;
        }
Example #2
0
        /// <summary>
        /// Executes an HTTP POST against the Url specified with the supplied post data, 
        /// returning the entire response body in string form.
        /// </summary>
        /// <param name="url">The URL to post to</param>
        /// <param name="postData">The x-www-form-urlencoded data to post to the URL</param>
        /// <param name="requireHttp200">
        /// Boolean indicating whether or not to return 
        /// null if the repsonse status code is not 200 (OK).
        /// </param>
        /// <returns>
        /// The response body or null if the response status is required to 
        /// be 200 (OK) but is not
        /// </returns>
        internal static string PerformHttpPost(string url, string postData, bool requireHttp200) {
            var restClient = new RestClient();
            BodyContent content = new WwwFormUrlEncodedContent(postData);

            var response = restClient.Post(url, content);

            if (requireHttp200 || response.StatusCode != HttpStatusCode.OK)
                return null;

            return response.Body;
        }
Example #3
0
        private static void RunTest(Dictionary<string, string> settings)
        {
            var uri = settings[UriName];
            var verb = settings[MethodName];
            var method = GetMethod(verb);
            var cookieValue = settings[CookieName];
            var payload = settings[PayloadName];
            var contentType = settings[ContentTypeName];
            var runCount = int.Parse(settings[RunCountName]);

            Console.WriteLine(UriName + ": " + uri);
            Console.WriteLine(MethodName + ": " + method);
            Console.WriteLine(CookieName + ": " + cookieValue);
            Console.WriteLine(PayloadName + ": " + payload);
            Console.WriteLine(ContentTypeName + ": " + contentType);
            Console.WriteLine(RunCountName + ": " + runCount);

            var cookie = new RequestHeader("Cookie", cookieValue);
            var content = new Everest.Content.StringBodyContent(payload, contentType);
            var client = new RestClient(cookie, new RequestHeader("X-Requested-With", "XMLHttpRequest"));

            var firstResponse = client.Send(method, uri, content);
            if (contentType == "application/json")
            {
                if (!firstResponse.Body.StartsWith("{"))
                {
                    throw new Exception("Epected JSON but got: " + firstResponse.Body);
                }
            }
            firstResponse.Dispose();

            var stopwatch = new Stopwatch();
            stopwatch.Start();
            for (var i = 0; i < runCount; i++)
            {
                client.Send(method, uri, content).Dispose();
                Console.Write(".");
            }
            stopwatch.Stop();

            Console.WriteLine();
            Console.WriteLine(uri);
            Console.WriteLine("Average time: " + new TimeSpan(stopwatch.Elapsed.Ticks/runCount));
        }