Dusan
Dusan

Reputation: 5144

How to compress HttpClient post and receive data in web api

I have the following web api client which sends data to server using json and gzip:

    public void Remote_Push(BlockList Blocks)
    {
        // Pushes pending records to the remote server
        using (var Client = new HttpClient())
        {
            Client.BaseAddress = new Uri(Context.ServerUrl);
            Client.DefaultRequestHeaders.Accept.Clear();
            Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            var Content = JsonCompress(Blocks);
            var T1 = Client.PostAsync("SyncPush/", Content); T1.Wait();
            T1.Result.EnsureSuccess();
        }
    }

    private static ByteArrayContent JsonCompress(object Data)
    {
        // Compress given data using gzip 
        var Bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(Data));
        using (var Stream = new MemoryStream())
        {
            using (var Zipper = new GZipStream(Stream, CompressionMode.Compress, true)) Zipper.Write(Bytes, 0, Bytes.Length);
            var Content = new ByteArrayContent(Stream.ToArray());
            Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            Content.Headers.ContentEncoding.Add("gzip");
            return Content;
        }
    }

On the server, I have created following action in web api controller:

    [HttpPost]
    public void SyncPush([FromBody]BlockList Blocks)
    {
        var Context = SyncCreateContext();
        var Sync = new Processor(Context);
        Sync.ServerPush(Blocks);
    }

Previously, I have used PostAsJsonAsync on the client and it worked fine.

Now, I have switched to ByteArrayContent and gzip and no longer works, the Blocks is always null on the server. What am I missing here, what is wrong or could be the problem?

Upvotes: 2

Views: 6048

Answers (2)

user2410689
user2410689

Reputation:

just FYI, since .NET Core is not covered and this question is still relevant working .NET Core code. I used brotli, since that is a widely accepted standard today.

using System.Text.Json.Serialization;
using System.Text.Json;
using System.Net.Http.Headers;
using System.IO.Compression;

public static class CompressedJsonHelper
{
private static readonly Lazy<JsonSerializerOptions>
    Options = new(() =>
    {
        var opt = new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
        //opt.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
        return opt;
    });  

public static HttpContent ToJson(this object data, bool noCompress = false)
{
    if (noCompress)
    {
        ByteArrayContent byteContent = new (ToBytes(data));
        byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        return byteContent;
    }
    MemoryStream memoryStream = new ();
    BrotliStream compress = new (memoryStream, CompressionLevel.Optimal, true);
    StreamContent streamContent = new (memoryStream);
    streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    streamContent.Headers.ContentEncoding.Add("brotli");
    JsonSerializer.Serialize(compress, data, Options.Value);
    compress.Flush();
    memoryStream.Position = 0;
    return streamContent;
}
private static byte[] ToBytes(this object data) => JsonSerializer.SerializeToUtf8Bytes(data, Options.Value);

}

and the httpClient code:

using HttpRequestMessage request = new(HttpMethod.Post, $"{yourbaseurl}/{path}")
{
     Content = json.ToJson()
};
await _httpClient.SendAsync(request, ...) etc

Upvotes: 1

Kosala W
Kosala W

Reputation: 2143

Here is a sample console application to do what you are trying to do.

      /*using System;
      using System.IO;
      using System.IO.Compression;
      using System.Net.Http;
      using System.Net.Http.Headers;
      using System.Text;
      using Newtonsoft.Json;
      using WebApi.Models;*/

 class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Press any key to POST");
        Console.ReadLine();
        RemotePush(new BlockList());
        Console.ReadLine();
    }

    public static async void RemotePush(BlockList blocks)
    {
        try
        {
            using (var client = new HttpClient())
            {
                try
                {
                    Console.WriteLine("Please wait.");
                    client.BaseAddress = new Uri("http://localhost:52521/Home/"); 
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    var content = JsonCompress(blocks);
                    var response = await client.PostAsync("SyncPush/", content);
                    using (var stream = await response.Content.ReadAsStreamAsync())
                    {
                        using (var streamReader = new StreamReader(stream))
                        {
                            Console.WriteLine(streamReader.ReadToEnd());
                        }
                    }
                    Console.WriteLine("Done.");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

    private static MultipartFormDataContent JsonCompress(object data)
    {
        var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data));
        var stream = new MemoryStream();
        using (var zipper = new GZipStream(stream, CompressionMode.Compress, true))
        {
            zipper.Write(bytes, 0, bytes.Length);
        }
        MultipartFormDataContent multipartContent = new MultipartFormDataContent();
        multipartContent.Add(new StreamContent(stream), "gzipContent");
        return multipartContent;
    }
}

My controller is like this.

    [System.Web.Mvc.HttpPost]
    public JsonResult SyncPush(BlockList content)
    {
        try
        {
            if (content != null)
            {
                return Json("success", JsonRequestBehavior.AllowGet);
            }
            return Json("failed due to null", JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex)
        {
            return Json("failed " + ex.Message, JsonRequestBehavior.AllowGet);
        }
    }

Upvotes: 2

Related Questions