Skip to content

Commit 6df208e

Browse files
committed
Resolving merge request #13
2 parents d5b0237 + 2baf9ca commit 6df208e

2 files changed

Lines changed: 53 additions & 6 deletions

File tree

cSharp/Api-ng-sample-code/Api-ng-sample-code/Json/JsonConvert.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Text;
55
using System.IO;
66
using Newtonsoft.Json;
7+
using System.IO.Compression;
78

89

910
namespace Api_ng_sample_code.Json
@@ -16,7 +17,35 @@ public static JsonResponse<T> Import<T>(TextReader reader)
1617
var jsonResponse = reader.ReadToEnd();
1718
return Deserialize<JsonResponse<T>>(jsonResponse);
1819
}
20+
public static JsonResponse<T> ImportGzip<T>(Stream reader)
21+
{
22+
var jsonResponse = decompress(reader);
23+
return Deserialize<JsonResponse<T>>(jsonResponse);
24+
}
1925

26+
public static string decompress(Stream reader)
27+
{
28+
using (GZipStream stream = new GZipStream(reader, CompressionMode.Decompress))
29+
{
30+
const int size = 4096;
31+
byte[] buffer = new byte[size];
32+
using (MemoryStream memory = new MemoryStream())
33+
{
34+
int count = 0;
35+
do
36+
{
37+
count = stream.Read(buffer, 0, size);
38+
if (count > 0)
39+
{
40+
memory.Write(buffer, 0, count);
41+
}
42+
}
43+
while (count > 0);
44+
return System.Text.ASCIIEncoding.ASCII.GetString(memory.ToArray());
45+
}
46+
47+
}
48+
}
2049
public static T Deserialize<T>(string json)
2150
{
2251
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);

cSharp/Api-ng-sample-code/Api-ng-sample-code/JsonRpcClient.cs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,35 @@ public T Invoke<T>(string method, IDictionary<string, object> args = null)
164164

165165
using (WebResponse response = GetWebResponse(request))
166166
using (Stream stream = response.GetResponseStream())
167-
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
168167
{
169-
var jsonResponse = JsonConvert.Import<T>(reader);
170-
// Console.WriteLine("\nGot Response: " + JsonConvert.Serialize<JsonResponse<T>>(jsonResponse));
171-
if (jsonResponse.HasError)
168+
if (response.ContentEncoding.ToLower() == "gzip")
172169
{
173-
throw ReconstituteException(jsonResponse.Error);
170+
var jsonResponse = JsonConvert.ImportGzip<T>(stream);
171+
Console.WriteLine("\nGot Response: " + JsonConvert.Serialize<JsonResponse<T>>(jsonResponse));
172+
if (jsonResponse.HasError)
173+
{
174+
throw ReconstituteException(jsonResponse.Error);
175+
}
176+
else
177+
{
178+
return jsonResponse.Result;
179+
}
174180
}
175181
else
176182
{
177-
return jsonResponse.Result;
183+
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
184+
{
185+
var jsonResponse = JsonConvert.Import<T>(reader);
186+
Console.WriteLine("\nGot Response: " + JsonConvert.Serialize<JsonResponse<T>>(jsonResponse));
187+
if (jsonResponse.HasError)
188+
{
189+
throw ReconstituteException(jsonResponse.Error);
190+
}
191+
else
192+
{
193+
return jsonResponse.Result;
194+
}
195+
}
178196
}
179197
}
180198
}

0 commit comments

Comments
 (0)