使用gzip编码时,WCF服务返回不正确的Content-Length

时间:2011-09-12 06:44:30

标签: ajax wcf json gzip webhttpbinding

我有一个包含过滤文本框和列表框的网页。对文本框的修改会触发AJAX请求,该请求返回一个值数组,用于填充列表框。

我有时遇到这些调用失败的问题,这取决于返回的数据大小。小尺寸的返回数据会导致错误,返回并成功处理大尺寸数据。

当我使用大于4.2的jQuery版本时,才会出现此问题。如果我使用jQuery版本4.2,我没有问题。

<小时/> 以下是电话会议的代码:

        jQuery.ajax(
            {
                cache: false,
                url: "../Services/CmsWebService.svc/GetAvailableVideosForCompany",
                type: "GET",
                complete: function (jqXHR, textStatus) {
                    var responseText = jqXHR.responseText;
                    jQuery('#debugConsole').text(responseText);
                    availableVideosPopulationState.isRunning = false;
                    setTimeout(populateAvailableVideosListBox, 100);
                },
                data: { "companyIdString": queryParameters.companyIdField,
                    "textFilter": queryParameters.filterText
                },
                dataType: 'json',
                error: function (jqXHR, textStatus, errorThrown) {
                    var errorString = 'Error thrown from ajax call: ' + textStatus + 'Error: ' + errorThrown;
                    alert(errorString);
                },
                success: function (data, textStatus, jqXHR) {
                    populateVideoListFromAjaxResults(data);
                }
            }
             );

如果返回两个元素,则以下是调试控制台的内容:

{"d":[{"__type":"ListEntry:#WebsitePresentationLayer","Text":"SOJACKACT0310DSN1.mpg - [SOJACKACT0310DSN1]","Value":"5565_5565"},{"__type":"ListEntry:#WebsitePresentationLayer","Text":"SOJACKACT0310DSN1Q.mpg - [SOJACKACT0310DSN1Q]","Value":"5566_5566"}]}

但是如果返回一个元素:

{"d":[{"__type":"

因此,当然,我们得到一个“未终止的字符串常量”错误。


我用fiddler做了一些调查。

在所有回复(即使是成功的回复)上,fiddler显示错误:

  

Fiddler在会话# n1 中检测到协议违规。

     

内容长度不匹配:响应标头指示 n2 字节,但是   服务器发送 n3 字节。

如果响应标头指示大于的大小而不是实际大小,则浏览器仍然可以解释结果。

如果响应标头指示小于的实际大小,则浏览器无法解释结果。

显而易见的假设是,响应处理代码读取Content-Length标头,并且不读取长度中规定的数据。

我调查的下一步是比较jQuery版本1.6.1(中断)和版本1.4.2(不会中断)的请求/响应头。

jQuery 1.6.1请求标题:

GET /Web/Services/CmsWebService.svc/GetAvailableVideosForCompany?companyIdString=2&textFilter=3DSBDL2&_=1315869366142 HTTP/1.1
X-Requested-With: XMLHttpRequest
Accept: application/json, text/javascript, */*; q=0.01
Referer: http://localhost:52200/Web/Admin/PlayerGroupEditor.aspx?groupid=76
Accept-Language: en-au
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Host: localhost:52200
Connection: Keep-Alive
Cookie: .ASPXAUTH=CE853BBD860F40F0026400610074006D006500640069006100310000002B5387799D71CC01002B5B5D62C771CC0100002F0000006B119589A7305098A560E57515498C56ECB332035F300427CDA2B28205D5E6B6

jQuery 1.6.1响应标头

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Mon, 12 Sep 2011 23:02:36 GMT
X-AspNet-Version: 4.0.30319
Content-Encoding: gzip
Content-Length: 140
Cache-Control: private
Content-Type: application/json; charset=utf-8
Connection: Close

这是我使用jQuery 1.4.1时的请求标头。请注意,Accept标头与jQuery 1.6.1值不同。

GET /Web/Services/CmsWebService.svc/GetAvailableVideosForCompany?_=1315870305531&companyIdString=2&textFilter=3DSBDL2 HTTP/1.1
Referer: http://localhost:52200/Web/Admin/PlayerGroupEditor.aspx?groupid=76
Content-Type: application/x-www-form-urlencoded
X-Requested-With: XMLHttpRequest
Accept: application/json, text/javascript, */*
Accept-Language: en-au
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Host: localhost:52200
Connection: Keep-Alive
Cookie: .ASPXAUTH=CE853BBD860F40F0026400610074006D006500640069006100310000002B5387799D71CC01002B5B5D62C771CC0100002F0000006B119589A7305098A560E57515498C56ECB332035F300427CDA2B28205D5E6B6

回复jQuery 4.1.1:

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Mon, 12 Sep 2011 23:31:46 GMT
X-AspNet-Version: 4.0.30319
Content-Length: 131
Cache-Control: private
Content-Type: application/json; charset=utf-8
Connection: Close

所以明显不同的是,当通过jQuery 1.6.1进行调用时,使用gzip压缩响应,并且当通过jQuery 1.4.2进行调用时,响应不会被压缩。

<小时/> 所以现在我可以解决方案,即覆盖默认的 Accept 标头,以确保它不包含"q=0.01"字符串。 (我可以找到"q=0.01"的最佳解释是here,但我不明白为什么我的服务实现会将此解释为严格压缩响应的请求。)

        // Make the AJAX call, passing in the company id and the filter string
        jQuery.ajax(
            {
                accepts: 'application/json, text/javascript, */*',
                cache: false,
                url: "../Services/CmsWebService.svc/GetAvailableVideosForCompany",
                type: "GET",
                complete: function (jqXHR, textStatus) {
                    var responseText = jqXHR.responseText;
                    jQuery('#debugConsole').text(responseText);
                    availableVideosPopulationState.isRunning = false;
                    setTimeout(populateAvailableVideosListBox, 100);
                },
                data: { "companyIdString": queryParameters.companyIdField,
                    "textFilter": queryParameters.filterText
                },
                dataType: 'json',
                error: function (jqXHR, textStatus, errorThrown) {
                    var errorString = 'Error thrown from ajax call: ' + textStatus + 'Error: ' + errorThrown;
                    alert(errorString);
                },
                success: function (data, textStatus, jqXHR) {
                    populateVideoListFromAjaxResults(data);
                }
            }
             );

所有这些调查之后,剩下的问题是为什么当响应是GZIP压缩时内容长度标题和实际内容长度之间存在差异?

我正在使用带有webHttpBinding的WCF服务。

2 个答案:

答案 0 :(得分:5)

首先 - 非常好的问题。这个问题为我提供了足够的信息来解决我的问题。

我有一个类似的问题,并在此处发布修复程序 - 以便它可以帮助某人。

  1. Ajax get&amp;邮件请求在IE中返回null

  2. 在其他浏览器中工作正常,但在fiddler中看到'Response Header指示n个字节,但服务器发送了nn个字节'消息。

  3.   

    显而易见的假设就是响应处理   代码读取Content-Length标头,不再读取任何数据

    我也这么认为!

    在这种情况下,我清楚了一件事。 某些内容正在篡改请求/响应。 我尝试切换回旧版本的jQuery(如你的问题所述),但这没有帮助。

    <强>修复 - 我打开了我的应用程序的Web配置,并通读了它。 模块中包含了来自telerik的'RadCompression模块',一旦删除它,一切都开始正常工作。

    已知RadCompression模块有问题,并且通过压缩响应会导致多个问题。

    如果您遇到类似问题,请尝试检查可能拦截您的请求/响应的内容。

答案 1 :(得分:1)

Response Header indicated 140 bytes, but server sent 254 bytes说了很多。是否与您使用的浏览器无关?如果是这样,我们可能会说IE或jQuery 1.4.3以及IE中的其他内容在读取响应标头中指定的字节数之后不会读取字节,而其他浏览器无论如何都会读取所有内容。

也可能(但我几乎不相信这一点)仅针对IE请求错误地形成响应头。然后,您必须查看IE与其他浏览器请求和服务代码之间的差异。也许您的服务专门处理IE请求?

计算JSON字符串中最后一个捕获引号(")之后的字节数将会很有趣。 114也许?