json ihttpmodule 压缩

发布于 2024-08-24 07:42:23 字数 2349 浏览 2 评论 0原文

我编写了一个 IHttpModule,它使用 gzip 压缩我的响应(我返回大量数据),以减少响应大小。 只要 Web 服务不抛出异常,它就可以很好地工作。 如果抛出异常,异常会被压缩,但内容编码标头会消失,并且客户端不知道如何读取异常。

我该如何解决这个问题?为什么标题丢失了?我需要在客户端获取异常。

这是模块:

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

这是网络服务:

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

我感谢任何帮助。

谢谢!

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.

Here is the module:

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

Here is the web service:

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

I appriciate any help.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江湖彼岸 2024-08-31 07:42:24

尽管您发布此问题已经有一段时间了,但我还是遇到了同样的问题,以下是我的修复方法:

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

app.Error += new EventHandler(app_Error);

在处理程序中,从 Response 中删除 Content-Type headers 并将 Response.Filter 属性设置为 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;
    }

}

也许有一种更优雅的方法可以做到这一点,但对我来说却很有效。

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:

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

app.Error += new EventHandler(app_Error);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文