Esempio n. 1
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;
        }
        public void CanConstructContentFromObjectProperties()
        {
            var objectSource = new
                {
                    Value1="Alpha",
                    Value2="Bravo",
                    Value3="Charlie",
                };

            var content = new WwwFormUrlEncodedContent(objectSource);

            Assert.That(content.MediaType, Is.EqualTo("application/x-www-form-urlencoded"));

            using (var reader = new StreamReader(content.AsStream()))
            {
                var contentString = reader.ReadToEnd();
                Assert.That(contentString, Is.EqualTo("Value1=Alpha&Value2=Bravo&Value3=Charlie"));
            }
        }
        public void CanConstructContentFromDictionaryWithCharactersNeedingEscaping()
        {
            var dictionary = new Dictionary<string, string>
                {
                    {"Value1", "Al&pha"},
                    {"Value2", "Bra<vo Beta"},
                    {"Value3", "Char\"lie"},
                };

            var content = new WwwFormUrlEncodedContent(dictionary);

            Assert.That(content.MediaType, Is.EqualTo("application/x-www-form-urlencoded"));

            using (var reader = new StreamReader(content.AsStream()))
            {
                var contentString = reader.ReadToEnd();
                Assert.That(contentString, Is.EqualTo("Value1=Al%26pha&Value2=Bra%3cvo+Beta&Value3=Char%22lie"));
            }
        }
        public void CanConstructContentFromDictionary()
        {
            var dictionary = new Dictionary<string, string>
                {
                    {"Value1", "Alpha"},
                    {"Value2", "Bravo"},
                    {"Value3", "Charlie"},
                };

            var content = new WwwFormUrlEncodedContent(dictionary);

            Assert.That(content.MediaType, Is.EqualTo("application/x-www-form-urlencoded"));

            using (var reader = new StreamReader(content.AsStream()))
            {
                var contentString = reader.ReadToEnd();
                Assert.That(contentString, Is.EqualTo("Value1=Alpha&Value2=Bravo&Value3=Charlie"));
            }
        }