public string PostWithDeflate(string url, string param)
        {
            byte[] jsonBytes    = Encoding.UTF8.GetBytes(param);
            var    deflateArray = new Compressor().DeflateReByte(jsonBytes);

            var uri     = url;
            var client  = new RestClient(uri);
            var request = new RestRequest(Method.POST);

            request.AddHeader("cache-control", "no-cache");
            request.AddHeader("content-type", "application/json");
            request.AddHeader("content-encoding", "deflate");
            request.AddParameter("application/json", deflateArray, ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);

            if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
            {
                throw new Exception(response.Content);
            }
            return(response.Content);
        }
Ejemplo n.º 2
0
        public async Task <string> PostWithDeflateByte(string url, string data)
        {
            using (HttpClientHandler handler = new HttpClientHandler())
            {
                handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                using (var client = new HttpClient(handler, false))
                {
                    var jsonBytes = Encoding.UTF8.GetBytes(data);
                    var ms        = new Compressor().DeflateReByte(jsonBytes);

                    var byteArray = new ByteArrayContent(ms);
                    byteArray.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                    byteArray.Headers.ContentEncoding.Add("deflate");

                    var response = await client.PostAsync(url, byteArray).ConfigureAwait(false);

                    var result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                    return(result);
                }
            }
        }