在Android的xamarin.forms中以https的Gzip格式发送JSON [英] Sending json as Gzip in https in xamarin.forms for android

查看:172
本文介绍了在Android的xamarin.forms中以https的Gzip格式发送JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过HTTPS向我的服务器发送一个剩余API请求,并在我的xamarin.forms android(操作系统为棉花糖)中得到一个json响应.

I am sending an rest API request to my server in HTTPS, and gets a json response, in my xamarin.forms android (OS is marshmallow).

是将json响应从服务器自动压缩到我的客户端,还是我需要在android的HttpClient类中定义一些内容才能对其进行压缩(我的互联网不好,所以压缩对于我...)

Is the json response automatically compressed from the server to my client, or do i need to define something in my HttpClient class, in the android, in order for it to be compressed (i have bad internet so the compression is important to me...)

推荐答案

解压缩:

为了在Xamarin.Forms中使用HttpClient使用压缩的JSON,您必须使用以下方式创建HttpClientHandler:

In order to consume compressed JSON using HttpClient in Xamarin.Forms you have to create a HttpClientHandler this way:

var httpHandler = new HttpClientHandler
{
    AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate
};
httpClient = new HttpClient(httpHandler);
await httpClient.GetStringAsync(url);

或者,您可以使用 ModernHttpClient ,它根据此线程.

Alternatively you can use ModernHttpClient which supports decompression out of the box according to this thread.

压缩:

要在Xamarin.Forms中启用压缩,您需要自己压缩请求内容.为此,我们扩展HttpContent:

To enable compression in Xamarin.Forms you need to compress the request content yourself. For this let's extend HttpContent:

public class JsonContent : HttpContent
    {
        private JsonSerializer serializer { get; }
        private object value { get; }

        public JsonContent(object value)
        {
            this.serializer = new JsonSerializer();
            this.value = value;
            Headers.ContentType = new MediaTypeHeaderValue("application/json");
            Headers.ContentEncoding.Add("gzip");
        }

        protected override bool TryComputeLength(out long length)
        {
            length = -1;
            return false;
        }

        protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
        {
            return Task.Factory.StartNew(() =>
            {
                using (var gzip = new GZipStream(stream, CompressionMode.Compress, true))
                using (var writer = new StreamWriter(gzip))
                {
                    serializer.Serialize(writer, value);
                }
            });
        }
    }

现在我们可以用JsonContent包装内容,并将其压缩后发送到我们的后端:

Now we can wrap our content with JsonContent and it will be sent compressed to our backend:

var jsonContent = new JsonContent(new List<string> { "a", "b", "c", "d", "e", "f" });
await httpClient.PostAsync(url, jsonContent));

后端:

从您的问题中我也了解到您不确定您的服务器"是否正在压缩响应.它应该很容易检查,检查您的响应是否包含Content-Encoding: gzip标头.

From your question I also understand that you are not sure if your 'server' is compressing the response. It should be very easy to check, check if your response contains Content-Encoding: gzip header.

PS:我在 github 上创建了一个示例项目,其中包含.NET Core带有GZip压缩/解压缩支持的MVC后端和Xamarin.Forms iOS前端,可以同时使用HttpClient和ModernHttpClient消费和发送压缩数据(使用GZip).

附加的屏幕:

P.S.: I created a sample project on github that contains a .NET Core MVC backend with GZip compression / decompression support and Xamarin.Forms iOS frontend which consumes and sends compressed data (using GZip) with both HttpClient and ModernHttpClient.

Screens attached:

这篇关于在Android的xamarin.forms中以https的Gzip格式发送JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆