示例#1
0
        /// <summary>
        /// Makes an HTTP with option JSON data serialized from an object
        /// and parses the result from JSON back into an object.
        /// Assumes that the service returns a JSON response
        /// </summary>
        /// <typeparam name="TResultType">The type of the object returned</typeparam>
        /// <param name="settings"><see cref="HttpRequestSettings"/>
        /// Configuration object for the HTTP request made to the server.
        /// </param>
        /// <returns>deserialized value/object from returned JSON data</returns>
        public static TResultType JsonRequest <TResultType>(HttpRequestSettings settings)
        {
            var client = new HttpUtilsWebClient(settings);

            client.Headers.Add("Accept", "application/json");

            string jsonResult;

            if (settings.Content != null)
            {
                if (!string.IsNullOrEmpty(settings.ContentType))
                {
                    client.Headers["Content-type"] = settings.ContentType;
                }
                else
                {
                    client.Headers["Content-type"] = "application/json;charset=utf-8;";
                }

                if (!settings.IsRawData)
                {
                    settings.CapturedRequestContent = JsonSerializationUtils.Serialize(settings.Content,
                                                                                       throwExceptions: true);
                }
                else
                {
                    settings.CapturedRequestContent = settings.Content as string;
                }

                jsonResult = client.UploadString(settings.Url, settings.HttpVerb, settings.CapturedRequestContent);

                if (jsonResult == null)
                {
                    return(default(TResultType));
                }
            }
            else
            {
                jsonResult = client.DownloadString(settings.Url);
            }

            settings.CapturedResponseContent = jsonResult;
            settings.Response = client.Response;

            return((TResultType)JsonSerializationUtils.Deserialize(jsonResult, typeof(TResultType), true));
        }
示例#2
0
        /// <summary>
        /// Makes an HTTP with option JSON data serialized from an object
        /// and parses the result from JSON back into an object.
        /// Assumes that the service returns a JSON response
        /// </summary>
        /// <typeparam name="TResultType">The type of the object returned</typeparam>
        /// <param name="settings"><see cref="HttpRequestSettings"/>
        /// Configuration object for the HTTP request made to the server.
        /// </param>
        /// <returns>deserialized value/object from returned JSON data</returns>
        public static TResultType JsonRequest <TResultType>(HttpRequestSettings settings)
        {
            var client = new HttpUtilsWebClient();

            if (settings.Credentials != null)
            {
                client.Credentials = settings.Credentials;
            }

            if (settings.Proxy != null)
            {
                client.Proxy = settings.Proxy;
            }

            client.Headers.Add("Accept", "application/json");

            if (settings.Headers != null)
            {
                foreach (var header in settings.Headers)
                {
                    client.Headers[header.Key] = header.Value;
                }
            }

            string jsonResult;

            if (settings.HttpVerb == "GET")
            {
                jsonResult = client.DownloadString(settings.Url);
            }
            else
            {
                if (!string.IsNullOrEmpty(settings.ContentType))
                {
                    client.Headers["Content-type"] = settings.ContentType;
                }
                else
                {
                    client.Headers["Content-type"] = "application/json";
                }

                if (!settings.IsRawData)
                {
                    settings.CapturedRequestContent = JsonSerializationUtils.Serialize(settings.Content, throwExceptions: true);
                }
                else
                {
                    settings.CapturedRequestContent = settings.Content as string;
                }

                jsonResult = client.UploadString(settings.Url, settings.HttpVerb, settings.CapturedRequestContent);

                if (jsonResult == null)
                {
                    return(default(TResultType));
                }
            }

            settings.CapturedResponseContent = jsonResult;
            settings.Response = client.Response;

            return((TResultType)JsonSerializationUtils.Deserialize(jsonResult, typeof(TResultType), true));
        }