JSON IHttpModule的COM pression [英] json ihttpmodule compression

查看:114
本文介绍了JSON IHttpModule的COM pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为了减少响应大小使用gzip(我回了大量数据)写了IHttpModule的该COM preSS我输入反应。
只要Web服务不抛出异常这是伟大的工作。
如果抛出异常,异常gzip压缩的,但内容编码头是消失,客户不知道读例外。

I wrote an IHttpModule that compress my respone using gzip (I return a lot of data) in order to reduce response size. It is working great as long as the web service doesn't throws an exception. In case exception is thrown, the exception gzipped but the Content-encoding header is disappear and the client doesn't know to read the exception.

我该如何解决呢?为什么头部丢失?我需要在客户端的除外。

How can I solve this? Why the header is missing? I need to get the exception in the client.

下面是模块:

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服务:

[WebMethod]
public void DoSomething()
{
    throw new Exception("This message get currupted on the client because the client doesn't know it gzipped.");
}

我appriciate任何帮助。

I appriciate any help.

谢谢!

推荐答案

尽管它已经有一段时间,因为你张贴了这个问题,我只是有同样的问题,这里是我如何固定它:

Even though it's been a while since you posted this question, I just had the same issue and here's how I fixed it:

在init()方法中,添加的处理程序错误事件

In the Init() method, add a handler for the Error event

app.Error += new EventHandler(app_Error);

在处理程序中,从响应头删除内容类型和设置Response.Filter属性设置为null。

In the handler, remove Content-Type from the Response headers and set the Response.Filter property to null.

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;
    }

}

也许有一个更优雅的方式来做到这一点,但做的把戏我。

Maybe there's a more elegant way to do this but did the trick for me.

这篇关于JSON IHttpModule的COM pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆