WCF REST服务 - 如何使用gzip解压缩表单数据?

时间:2012-12-10 12:40:42

标签: wcf rest gzip compression

我们有一个WCF-REST服务,客户端使用Content Encoding = gzip发送数据并以gzip格式压缩数据。但是,我们无法从WCF服务中收到的请求中解压缩表单数据。

1 个答案:

答案 0 :(得分:1)

最后,我的一个拼贴画找到了答案,谢谢Sandesh和团队!!!

您需要添加拦截每个HTTP请求并解压缩数据的IHttpModule

/// <summary>
    /// This class Handles various pre-conditions which has to performed before processing the HTTP request.
    /// @author XXXXX
    /// </summary>
    public class PreRequestHandler : IHttpModule
    {
        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void Init(HttpApplication httpContext)
        {
            httpContext.BeginRequest += DecompressReceivedRequest;
        }

        /// <summary>
        /// Decompresses the HTTP request before processing it.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void DecompressReceivedRequest(object sender, EventArgs e)
        {
            HttpApplication httpApp = (HttpApplication)sender;

            if ("gzip" == httpApp.Request.Headers["Content-Encoding"])
            {
                httpApp.Request.Filter = new GZipStream(httpApp.Request.Filter, CompressionMode.Decompress);
            }

        }
    }

此外,需要在web.config文件中添加以下条目

  <!-- Configuration setting to add Custom Http Module to handle various pre-conditions which has to performed before processing the HTTP request.-->
  <system.webServer>
    <modules>
      <add name="PreRequestHandler" type="Your service class.PreRequestHandler"/>
    </modules>
  </system.webServer>