From c726f93ffbb6f056797f090f5c8bf5123133f26a Mon Sep 17 00:00:00 2001 From: bd555 Date: Fri, 1 May 2015 09:34:31 +0300 Subject: [PATCH 1/2] gzip added --- .../Api-ng-sample-code/JsonRpcClient.cs | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/cSharp/Api-ng-sample-code/Api-ng-sample-code/JsonRpcClient.cs b/cSharp/Api-ng-sample-code/Api-ng-sample-code/JsonRpcClient.cs index c37155a..0cd115c 100644 --- a/cSharp/Api-ng-sample-code/Api-ng-sample-code/JsonRpcClient.cs +++ b/cSharp/Api-ng-sample-code/Api-ng-sample-code/JsonRpcClient.cs @@ -126,6 +126,7 @@ protected WebRequest CreateWebRequest(Uri uri) request.Method = "POST"; request.ContentType = "application/json-rpc"; request.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8"); + request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip"); request.Headers.Add(CustomHeaders); return request; } @@ -151,17 +152,35 @@ public T Invoke(string method, IDictionary args = null) using (WebResponse response = GetWebResponse(request)) using (Stream stream = response.GetResponseStream()) - using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { - var jsonResponse = JsonConvert.Import(reader); - Console.WriteLine("\nGot Response: " + JsonConvert.Serialize>(jsonResponse)); - if (jsonResponse.HasError) + if (response.ContentEncoding.ToLower() == "gzip") { - throw ReconstituteException(jsonResponse.Error); + var jsonResponse = JsonConvert.ImportGzip(stream); + Console.WriteLine("\nGot Response: " + JsonConvert.Serialize>(jsonResponse)); + if (jsonResponse.HasError) + { + throw ReconstituteException(jsonResponse.Error); + } + else + { + return jsonResponse.Result; + } } else { - return jsonResponse.Result; + using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) + { + var jsonResponse = JsonConvert.Import(reader); + Console.WriteLine("\nGot Response: " + JsonConvert.Serialize>(jsonResponse)); + if (jsonResponse.HasError) + { + throw ReconstituteException(jsonResponse.Error); + } + else + { + return jsonResponse.Result; + } + } } } } From 2baf9ca24815e83bbe1eccf90f10072e9b4dc52f Mon Sep 17 00:00:00 2001 From: bd555 Date: Fri, 1 May 2015 09:36:36 +0300 Subject: [PATCH 2/2] gzip added --- .../Api-ng-sample-code/Json/JsonConvert.cs | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/cSharp/Api-ng-sample-code/Api-ng-sample-code/Json/JsonConvert.cs b/cSharp/Api-ng-sample-code/Api-ng-sample-code/Json/JsonConvert.cs index 4342cdf..5a81298 100644 --- a/cSharp/Api-ng-sample-code/Api-ng-sample-code/Json/JsonConvert.cs +++ b/cSharp/Api-ng-sample-code/Api-ng-sample-code/Json/JsonConvert.cs @@ -4,6 +4,7 @@ using System.Text; using System.IO; using Newtonsoft.Json; +using System.IO.Compression; namespace Api_ng_sample_code.Json @@ -16,7 +17,35 @@ public static JsonResponse Import(TextReader reader) var jsonResponse = reader.ReadToEnd(); return Deserialize>(jsonResponse); } + public static JsonResponse ImportGzip(Stream reader) + { + var jsonResponse = decompress(reader); + return Deserialize>(jsonResponse); + } + public static string decompress(Stream reader) + { + using (GZipStream stream = new GZipStream(reader, CompressionMode.Decompress)) + { + const int size = 4096; + byte[] buffer = new byte[size]; + using (MemoryStream memory = new MemoryStream()) + { + int count = 0; + do + { + count = stream.Read(buffer, 0, size); + if (count > 0) + { + memory.Write(buffer, 0, count); + } + } + while (count > 0); + return System.Text.ASCIIEncoding.ASCII.GetString(memory.ToArray()); + } + + } + } public static T Deserialize(string json) { return Newtonsoft.Json.JsonConvert.DeserializeObject(json);