| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | using System.IO; |
| | using System.Net.Http; |
| | using System.Net.Http.Headers; |
| | using System.Text; |
| |
|
| | using System.IO.Compression; |
| |
|
| | using Google.Apis.Services; |
| |
|
| | namespace Google.Apis.Requests |
| | { |
| | |
| | static class HttpRequestMessageExtenstions |
| | { |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | 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; |
| | } |
| |
|
| | |
| | |
| | |
| | internal static HttpContent CreateZipContent(string content) |
| | { |
| | var stream = CreateGZipStream(content); |
| | var sc = new StreamContent(stream); |
| | sc.Headers.ContentEncoding.Add("gzip"); |
| | return sc; |
| | } |
| |
|
| | |
| | private static Stream CreateGZipStream(string serializedObject) |
| | { |
| | byte[] bytes = System.Text.Encoding.UTF8.GetBytes(serializedObject); |
| | using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) |
| | { |
| | using (GZipStream gzip = new GZipStream(ms, CompressionMode.Compress, true)) |
| | { |
| | gzip.Write(bytes, 0, bytes.Length); |
| | } |
| |
|
| | |
| | ms.Position = 0; |
| | byte[] compressed = new byte[ms.Length]; |
| | ms.Read(compressed, 0, compressed.Length); |
| | return new MemoryStream(compressed); |
| | } |
| | } |
| | } |
| | } |
| |
|