压缩适用于XML,但不适用于使用IIS 7.5的JSON

时间:2016-02-23 07:54:31

标签: c# json xml iis-7.5 gzip

我试图让我的网络服务使用gzip发送其JSON响应,但它从不压缩JSON响应。但是当我将ContentType从application / json更改为text / plain时,gzip压缩工作正常。

ContentType = "application/json" - Compression does not work
ContentType = "text/xml" - Compression works
ContentType = "text/plain" - Compression works

我使用Fiddler检查HTTP请求,我可以验证我是否使用Accept-Encoding发送请求:gzip,deflate标头集。我也尝试过将Accept: / 设置为true。

HEADER REQUEST:

*Proxy-Connection: keep-alive
Content-Length: 74
Cache-Control: max-age=0
Accept:     text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6*

服务器响应:

*HTTP/1.1 200 OK
Via: 1.1 EADIPROXY2
Connection: Keep-Alive
Proxy-Connection: Keep-Alive
Content-Length: 1424
Date: Tue, 23 Feb 2016 07:35:23 GMT
Content-Type: application/json
Server: Microsoft-IIS/7.5
Cache-Control: private, max-age=0
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET*

在applicationHost.config中添加或删除这些行似乎对XML的压缩和JSON的非压缩没有影响。

<staticTypes>
            <add mimeType="text/*" enabled="true" />
            <add mimeType="message/*" enabled="true" />
            <add mimeType="application/json" enabled="true" />
            <add mimeType="application/json; charset=utf-8" enabled="true" />
        <add mimeType="application/atom+xml" enabled="true" />
                <add mimeType="application/atom+xml;charset=utf-8" enabled="true" />
                <add mimeType="application/xaml+xml" enabled="true" />
            <add mimeType="application/javascript" enabled="true" />
            <add mimeType="application/x-javascript" enabled="true" />
            <add mimeType="application/x-javascript; charset=utf-8" enabled="true" />
            <add mimeType="*/*" enabled="false" />
            <add mimeType="application/*" enabled="true" />
        </staticTypes>
        <dynamicTypes>
            <add mimeType="text/*" enabled="true" />
            <add mimeType="message/*" enabled="true" />
            <add mimeType="application/json" enabled="true" />
            <add mimeType="application/json; charset=utf-8" enabled="true" />
            <add mimeType="application/javascript" enabled="true" />
            <add mimeType="application/x-javascript" enabled="true" />
            <add mimeType="application/x-javascript; charset=utf-8" enabled="true" />
            <add mimeType="*/*" enabled="false" />
            <add mimeType="application/atom+xml" enabled="true" />
            <add mimeType="application/atom+xml;charset=utf-8" enabled="true" />
        </dynamicTypes>
    </httpCompression>

我在IIS中禁用了动态压缩,因为它将{d:null}标记附加到JSON响应的末尾,并且压缩无论如何都不起作用。

0 个答案:

没有答案