asp.net webservice处理gzip压缩请求 [英] asp.net webservice handling gzip compressed request

查看:31
本文介绍了asp.net webservice处理gzip压缩请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个 asp.net .asmx 网络服务来处理来自第三方工具的请求.第三方工具向 Web 服务发出 http POST 请求以获取用户信息.我正在使用 IIS7

I have an asp.net .asmx webservice written to handle requests from a third party tool. The third party tool makes an http POST request to the webservice to get user information. I'm using IIS7

在选中删除所有编码"的情况下运行 Fiddler,我可以看到 Web 服务调用,并且一切正常.如果我取消选中删除所有编码",则 Web 服务调用将失败并显示 400 错误请求.我看到的不同之处在于 Fiddler 正在删除标题Content-Encoding:gzip"并且正在解压缩内容.

Running Fiddler with "Remove All Encodings" checked, I can see the webservice call and and everything functions properly. If I uncheck "Remove All Encodings", the webservice call fails with a 400 Bad Request. The difference I see is that the header "Content-Encoding: gzip" is being removed by Fiddler and the content is being decompressed.

所以,当 Content-Encoding 标头被移除并且内容被解压后,我的 web 服务就可以完美运行了.当 header 存在并且内容被压缩时,webservice 会失败.

So, when the Content-Encoding header is removed and the content is decompressed, my webservice functions perfectly. When the header is present and the content is compressed, the webservice fails.

我该怎么做:

  1. 配置我的网络服务以告诉客户端它不会接受压缩请求(并希望第三方工具尊重这一点)
  2. 在 asp.net 处理的早期解压内容
  3. 修改我的网络服务以使用压缩数据

更新:需要明确的是,我不需要在响应中配置 gzip 编码,我需要处理对我的 web 服务的 gzip 编码的请求.

Update: To be clear, I don't need to configure gzip encoding in the Response, I need to deal with a Request TO my webservice that is gzip encoded.

更新 2:第三方工具是 Salesforce.com Outlook 插件.因此,我无权对其进行修改,并且许多其他公司都在使用它而不会遇到麻烦.这一定是我正在做(或不做)的事情

Update 2: The third-party tool is the Salesforce.com Outlook plugin. So, I don't have access to modify it and it is used by many other companies without trouble. It's got to be something I'm doing (or not doing)

更新 3:我在这里找到了一篇帖子说 IIS 不支持带有压缩数据的传入 POST 请求,它只支持压缩响应.这仍然是真的吗?

Update 3: I found one post here that says that IIS does not support incoming POST requests with compressed data, it only supports compressed Responses. Can this still be true?

推荐答案

我找到了部分答案 这里.

class DecompressStream : Stream
{
    ...

    public override int Read(byte[] buffer, int offset, int count)
    {
        GZipStream test = new GZipStream(_sink, CompressionMode.Decompress);

        int c = test.Read(buffer, offset, count);

        return c;
    }

    ...
}

然后我可以像这样在请求对象上指定过滤器:

I can then specify the filter on the request object like this:

void Application_BeginRequest(object sender, EventArgs e)
    {
        string contentEncoding = Request.Headers["Content-Encoding"];
        Stream prevCompressedStream = Request.Filter;

        if(contentEncoding == null || contentEncoding.Length == 0)
            return;

        contentEncoding = contentEncoding.ToLower();

        if(contentEncoding.Contains("gzip"))
        {
            Request.Filter = new DecompressStream(Request.Filter);
        }
    }

我说部分回答是因为即使我现在可以处理传入的请求,即使响应未编码,响应也会获得Content-Encoding: gzip"标头.我可以在 Fiddler 中验证内容未编码.

I say partial answer because even though I can now process the incoming request, the response is getting a "Content-Encoding: gzip" header even though the response is not encoded. I can verify in Fiddler that the content is not encoded.

如果我对响应进行编码,则 Web 服务的客户端会失败.似乎即使它正在发送Accept-Encoding: gzip",它实际上并不接受 gzip 压缩响应.我可以在 Fiddler 中验证响应是否被压缩,Fiddler 会成功解压.

If I do encode the response, the client for the webservice fails. It seems that even though it is sending "Accept-Encoding: gzip", it does not in fact accept gzip compressed response. I can verify in Fiddler that the response is compressed and Fiddler will decompress it successfully.

所以,现在我一直在尝试从响应中删除一个杂散的Content-Encoding: gzip"标头.我已经从应用程序、web.config 和 IIS 中删除了所有我能找到的压缩参考.

So, now I'm stuck trying to get a stray "Content-Encoding: gzip" header removed from the response. I've removed all references I can find to compression from the application, the web.config, and IIS.

这篇关于asp.net webservice处理gzip压缩请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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