/// <summary>
        /// Sets the content of the request by the given body and the the required GZip configuration.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="service">The service.</param>
        /// <param name="body">The body of the future request. If <c>null</c> do nothing.</param>
        /// <param name="gzipEnabled">
        /// Indicates if the content will be wrapped in a GZip stream, or a regular string stream will be used.
        /// </param>
        internal static void SetRequestSerailizedContent(this HttpRequestMessage request,
            IClientService service, object body, bool gzipEnabled)
        {
            if (body == null)
            {
                return;
            }
            HttpContent content = null;

            var mediaType = "application/" + service.Serializer.Format;
            var serializedObject = service.SerializeObject(body);
            if (gzipEnabled)
            {
                content = CreateZipContent(serializedObject);
                content.Headers.ContentType = new MediaTypeHeaderValue(mediaType)
                {
                    CharSet = Encoding.UTF8.WebName
                };
            }
            else
            {
                content = new StringContent(serializedObject, Encoding.UTF8, mediaType);
            }

            request.Content = content;
        }
Beispiel #2
0
        /// <summary>
        /// Sets the content of the request by the given body and the the required GZip configuration.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="service">The service.</param>
        /// <param name="body">The body of the future request. If <c>null</c> do nothing.</param>
        /// <param name="gzipEnabled">
        /// Indicates if the content will be wrapped in a GZip stream, or a regular string stream will be used.
        /// </param>
        internal static void SetRequestSerailizedContent(this HttpRequestMessage request,
                                                         IClientService service, object body, bool gzipEnabled)
        {
            if (body == null)
            {
                return;
            }
            HttpContent content = null;

            var mediaType        = "application/" + service.Serializer.Format;
            var serializedObject = service.SerializeObject(body);

            if (gzipEnabled)
            {
                content = CreateZipContent(serializedObject);
                content.Headers.ContentType = new MediaTypeHeaderValue(mediaType)
                {
                    CharSet = Encoding.UTF8.WebName
                };
            }
            else
            {
                content = new StringContent(serializedObject, Encoding.UTF8, mediaType);
            }

            request.Content = content;
        }
        /// <summary>
        /// Returns the serialized version of the body, or null if unavailable.
        /// </summary>
        private string GetSerializedBody()
        {
            object body = GetBody();

            if (body == null)
            {
                return(null);
            }

            // Serialize the body.
            return(service.SerializeObject(body));
        }