How do enable a .Net web-API to accept g-ziped posts

后端 未结 4 1529
别跟我提以往
别跟我提以往 2020-12-08 11:58

I have a fairly bog standard .net MVC 4 Web API application.

 public class LogsController : ApiController
{

    public HttpResponseMessage PostLog(List<         


        
相关标签:
4条回答
  • 2020-12-08 12:11

    try this

        public class DeCompressedContent : HttpContent
    {
        private HttpContent originalContent;
        private string encodingType;
    
        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        /// <param name="encodingType"></param>
        public DeCompressedContent(HttpContent content, string encodingType)
        {
    
            if (content == null) throw new ArgumentNullException("content");
            if (string.IsNullOrWhiteSpace(encodingType)) throw new ArgumentNullException("encodingType");
    
            this.originalContent = content;
            this.encodingType = encodingType.ToLowerInvariant();
    
            if (!this.encodingType.Equals("gzip", StringComparison.CurrentCultureIgnoreCase) && !this.encodingType.Equals("deflate", StringComparison.CurrentCultureIgnoreCase))
            {
                throw new InvalidOperationException(string.Format("Encoding {0} is not supported. Only supports gzip or deflate encoding", this.encodingType));
            }
    
            foreach (KeyValuePair<string, IEnumerable<string>> header in originalContent.Headers)
            {
                this.Headers.TryAddWithoutValidation(header.Key, header.Value);
            }
    
            this.Headers.ContentEncoding.Add(this.encodingType);
        }
    
        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
        {
            var output = new MemoryStream();
    
            return this.originalContent
                .CopyToAsync(output).ContinueWith(task =>
                {
                    // go to start
                    output.Seek(0, SeekOrigin.Begin);
    
                    if (this.encodingType.Equals("gzip", StringComparison.CurrentCultureIgnoreCase))
                    {
                        using (var dec = new GZipStream(output, CompressionMode.Decompress))
                        {
                            dec.CopyTo(stream);
                        }
                    }
                    else
                    {
                        using (var def = new DeflateStream(output, CompressionMode.Decompress))
                        {
                            def.CopyTo(stream);
                        }
                    }
    
                    if (output != null)
                        output.Dispose();
                });
    
    
        }
    
        /// <summary>
        /// 
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        protected override bool TryComputeLength(out long length)
        {
            length = -1;
    
            return (false);
        }
    }
    
    0 讨论(0)
  • 2020-12-08 12:25

    I had the same requirement to POST gzipped data to a .NET web api controller. I came up with this solution:

    public class GZipToJsonHandler : DelegatingHandler
    {
        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
                                                               CancellationToken cancellationToken)
        {
            // Handle only if content type is 'application/gzip'
            if (request.Content.Headers.ContentType == null ||
                request.Content.Headers.ContentType.MediaType != "application/gzip")
            {
                return base.SendAsync(request, cancellationToken);
            }
    
            // Read in the input stream, then decompress in to the outputstream.
            // Doing this asynronously, but not really required at this point
            // since we end up waiting on it right after this.
            Stream outputStream = new MemoryStream();
            Task task = request.Content.ReadAsStreamAsync().ContinueWith(t =>
                {
                    Stream inputStream = t.Result;
                    var gzipStream = new GZipStream(inputStream, CompressionMode.Decompress);
    
                    gzipStream.CopyTo(outputStream);
                    gzipStream.Dispose();
    
                    outputStream.Seek(0, SeekOrigin.Begin);
                });
    
            // Wait for inputstream and decompression to complete. Would be nice
            // to not block here and work async when ready instead, but I couldn't 
            // figure out how to do it in context of a DelegatingHandler.
            task.Wait();
    
            // This next section is the key...
    
            // Save the original content
            HttpContent origContent = request.Content;
    
            // Replace request content with the newly decompressed stream
            request.Content = new StreamContent(outputStream);
    
            // Copy all headers from original content in to new one
            foreach (var header in origContent.Headers)
            {
                request.Content.Headers.Add(header.Key, header.Value);
            }
    
            // Replace the original content-type with content type
            // of decompressed data. In our case, we can assume application/json. A
            // more generic and reuseable handler would need some other 
            // way to differentiate the decompressed content type.
            request.Content.Headers.Remove("Content-Type");
            request.Content.Headers.Add("Content-Type", "application/json");
    
            return base.SendAsync(request, cancellationToken);
        }
    }
    

    Using this approach, the existing controller, which normally works with JSON content and automatic model binding, continued to work without any changes.

    I'm not sure why the other answer was accepted. It provides a solution for handling the responses (which is common), but not requests (which is uncommon). The Accept-Encoding header is used to specify acceptable response encodings, and is not related to request encodings.

    0 讨论(0)
  • 2020-12-08 12:25

    While Web API doesn't support Accept-Encoding header out of the box, but Kiran has a terrific blog post on how to do that - http://blogs.msdn.com/b/kiranchalla/archive/2012/09/04/handling-compression-accept-encoding-sample.aspx - using a custom MessageHandler

    If you implement his solution, all you need to do is issue a request with Accept-Encoding: gzip or Accept-Encoding: deflate header and the Web API response will be compressed in the message handler for you.

    0 讨论(0)
  • 2020-12-08 12:28

    I believe the correct answer is Kaliatech's and I would have left this as a comment and voted his up is I had enough reputation points, since I think his is basically correct.

    However, my situation called for the need to look at the encoding type type rather than the content type. Using this approach the calling system can still specify that the content type is json/xml/etc in the content type, but specify that the data is encoded using gzip or potentially another encoding/compression mechanism. This prevented me from needing to change the content type after decoding the input and allows any content type information to flow through in its original state.

    Here's the code. Again, 99% of this is Kaliatech's answer including the comments, so please vote his post up if this is useful.

    public class CompressedRequestHandler : DelegatingHandler
    {
        protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
        {
            if (IsRequetCompressed(request))
            {
                request.Content = DecompressRequestContent(request);
            }
    
            return base.SendAsync(request, cancellationToken);
        }
    
        private bool IsRequetCompressed(HttpRequestMessage request)
        {
            if (request.Content.Headers.ContentEncoding != null &&
                request.Content.Headers.ContentEncoding.Contains("gzip"))
            {
                return true;
            }
    
            return false;
        }
    
        private HttpContent DecompressRequestContent(HttpRequestMessage request)
        {
            // Read in the input stream, then decompress in to the outputstream.
            // Doing this asynronously, but not really required at this point
            // since we end up waiting on it right after this.
            Stream outputStream = new MemoryStream();
            Task task = request.Content.ReadAsStreamAsync().ContinueWith(t =>
                {
                    Stream inputStream = t.Result;
                    var gzipStream = new GZipStream(inputStream, CompressionMode.Decompress);
    
                    gzipStream.CopyTo(outputStream);
                    gzipStream.Dispose();
    
                    outputStream.Seek(0, SeekOrigin.Begin);
                });
    
            // Wait for inputstream and decompression to complete. Would be nice
            // to not block here and work async when ready instead, but I couldn't 
            // figure out how to do it in context of a DelegatingHandler.
            task.Wait();
    
            // Save the original content
            HttpContent origContent = request.Content;
    
            // Replace request content with the newly decompressed stream
            HttpContent newContent = new StreamContent(outputStream);
    
            // Copy all headers from original content in to new one
            foreach (var header in origContent.Headers)
            {
                newContent.Headers.Add(header.Key, header.Value);
            }
    
            return newContent;
        }
    

    I then registered this handler globally, which could be a dicey proposition if you are vulnerable to DoS attacks, but our service is locked down, so it works for us

    GlobalConfiguration.Configuration.MessageHandlers.Add(new CompressedRequestHandler());
    
    0 讨论(0)
提交回复
热议问题