Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added GZip to cSharp #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions cSharp/Api-ng-sample-code/Api-ng-sample-code/Json/JsonConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.IO;
using Newtonsoft.Json;
using System.IO.Compression;


namespace Api_ng_sample_code.Json
Expand All @@ -16,7 +17,35 @@ public static JsonResponse<T> Import<T>(TextReader reader)
var jsonResponse = reader.ReadToEnd();
return Deserialize<JsonResponse<T>>(jsonResponse);
}
public static JsonResponse<T> ImportGzip<T>(Stream reader)
{
var jsonResponse = decompress(reader);
return Deserialize<JsonResponse<T>>(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<T>(string json)
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
Expand Down
31 changes: 25 additions & 6 deletions cSharp/Api-ng-sample-code/Api-ng-sample-code/JsonRpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -151,17 +152,35 @@ public T Invoke<T>(string method, IDictionary<string, object> args = null)

using (WebResponse response = GetWebResponse(request))
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
var jsonResponse = JsonConvert.Import<T>(reader);
Console.WriteLine("\nGot Response: " + JsonConvert.Serialize<JsonResponse<T>>(jsonResponse));
if (jsonResponse.HasError)
if (response.ContentEncoding.ToLower() == "gzip")
{
throw ReconstituteException(jsonResponse.Error);
var jsonResponse = JsonConvert.ImportGzip<T>(stream);
Console.WriteLine("\nGot Response: " + JsonConvert.Serialize<JsonResponse<T>>(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<T>(reader);
Console.WriteLine("\nGot Response: " + JsonConvert.Serialize<JsonResponse<T>>(jsonResponse));
if (jsonResponse.HasError)
{
throw ReconstituteException(jsonResponse.Error);
}
else
{
return jsonResponse.Result;
}
}
}
}
}
Expand Down