1

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

4

1 回答 1

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>
于 2013-05-02T07:45:11.580 回答