首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >json ihttpmodule压缩

json ihttpmodule压缩
EN

Stack Overflow用户
提问于 2010-03-09 07:30:39
回答 1查看 1.3K关注 0票数 1

我写了一个IHttpModule,用gzip压缩我的响应(我返回了很多数据),以减小响应大小。只要web服务不抛出异常,它就工作得很好。如果抛出了异常,异常会被压缩,但是Content-encoding头消失了,客户端不知道要读取异常。

我该如何解决这个问题呢?为什么报头会丢失?我需要在客户端中获取异常。

下面是模块:

代码语言:javascript
复制
public class JsonCompressionModule : IHttpModule
{
    public JsonCompressionModule()
    {
    }

    public void Dispose()
    {
    }

    public void Init(HttpApplication app)
    {
        app.BeginRequest += new EventHandler(Compress);
    }

    private void Compress(object sender, EventArgs e)
    {
        HttpApplication app = (HttpApplication)sender;
        HttpRequest request = app.Request;
        HttpResponse response = app.Response;
        try
        {
            //Ajax Web Service request is always starts with application/json
            if (request.ContentType.ToLower(CultureInfo.InvariantCulture).StartsWith("application/json"))
            {
                //User may be using an older version of IE which does not support compression, so skip those
                if (!((request.Browser.IsBrowser("IE")) && (request.Browser.MajorVersion <= 6)))
                {
                    string acceptEncoding = request.Headers["Accept-Encoding"];

                    if (!string.IsNullOrEmpty(acceptEncoding))
                    {
                        acceptEncoding = acceptEncoding.ToLower(CultureInfo.InvariantCulture);

                        if (acceptEncoding.Contains("gzip"))
                        {
                            response.AddHeader("Content-encoding", "gzip");
                            response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
                        }
                        else if (acceptEncoding.Contains("deflate"))
                        {
                            response.AddHeader("Content-encoding", "deflate");
                            response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            int i = 4;
        }
    }
}

下面是web服务:

代码语言:javascript
复制
[WebMethod]
public void DoSomething()
{
    throw new Exception("This message get currupted on the client because the client doesn't know it gzipped.");
}

我希望能得到任何帮助。

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2011-07-29 02:00:27

虽然你发布这个问题已经有一段时间了,但我还是遇到了同样的问题,下面是我是如何解决它的:

在Init()方法中,为错误事件添加一个处理程序

代码语言:javascript
复制
app.Error += new EventHandler(app_Error);

在处理程序中,从响应标头中移除Content-Type,并将Response.Filter属性设置为null。

代码语言:javascript
复制
void app_Error(object sender, EventArgs e)
{

    HttpApplication httpApp = (HttpApplication)sender;
    HttpContext ctx = HttpContext.Current;

    string encodingValue = httpApp.Response.Headers["Content-Encoding"];

    if (encodingValue == "gzip" || encodingValue == "deflate")
    {
        httpApp.Response.Headers.Remove("Content-Encoding");
        httpApp.Response.Filter = null;
    }

}

也许有一种更优雅的方法可以做到这一点,但我做到了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2405595

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档