1

我开发了一个解决方案,它依赖于 AJAX 调用来检索信息并每 10 秒更新一次客户端页面。这工作正常,但我担心代码的可伸缩性,因为标头的数量和长度从客户端传递到服务器并再次返回。我已经删除了服务器端的一些冗余标头,主要是与 ASP.NET 相关的,现在我正试图减少来自客户端的标头。

我公司使用的浏览器是IE(6版本,即将升级到7)。这是我当前代码的近似值:

var xmlHTTP = new ActiveXObject('Microsoft.XMLHTTP');

xmlHTTP.onreadystatechange = function() {
    if ((xmlHTTP.readyState == 4) && (xmlHTTP.status == 200)) {
        myCallbackFunction(xmlHTTP);
    }
};

xmlHTTP.open('GET', 'myUrl.aspx');

try {
    xmlHTTP.setRequestHeader("User-Agent", ".");
    xmlHTTP.setRequestHeader("Accept", ".");
    xmlHTTP.setRequestHeader("Accept-Language", ".");
    xmlHTTP.setRequestHeader("Content-Type", ".");
} catch(e) {}

xmlHTTP.send();

虽然我已经读过可以清除其中一些标头,但我还没有找到一种在 IE6 中有效的方法。将它们设置为 null 会导致类型不匹配异常,因此我最终将它们替换为 '.' 暂且。是否有另一种清除它们的方法或减少提交的 HTTP 标头的替代方法?

此外,似乎根本无法替换或缩短“推荐人”标题。

4

3 回答 3

3

根据WD 规格

如果作为参数给出的 HTTP 标头已经是请求标头列表的一部分,则 setRequestHeader() 方法会附加一个值。

也就是说,您只能添加标题,而不能替换它们。

This doesn't completely match current browser behaviour, but it may be where browsers will be headed, in which case any efforts on this front are a waste of time in the long term. In any case current browser behaviour with setting headers is very varied and generally can't be relied upon.

There seems to be no way of replacing or shortening the 'Referrer' header at all.

That wouldn't surprise me, given that some people misguidedly use ‘Referer’ [sic] as an access-control mechanism.

You could try to make sure that the current page URL wasn't excessively long, but to be honest all this smells of premature optimisation to me. Whatever you do your request is going to fit within one IP packet, so there's not gonig to be a big visible performance difference.

It may be worthwhile for Mibbit (as mentioned on the blog you linked) to try this stuff, because Mibbit draws a quite staggering amount of traffic, but for a simple company-wide application I don't think the cross-browser-and-proxy-testing-burden:end-user-benefit ratio of messing with the headers is worth it.

于 2009-03-02T12:21:41.173 回答
0

IE 6 and older versions use the ActiveXObject created from MSXML.XMLHTTP (actually derived from IXMLHTTPRequest), whereas IE 7 and other modern browsers such as Mozilla use an intrinsic object called XmlHttpRequest. This is probably the reason why you cannot set the Request headers to null for the MSXML implementation but you can for the in-built object.

Therefore, I do not believe that there is any way to collectively clear all the headers. The link to Mibbit that you present only provides a function to set all the headers to null one by one. For normal scenarios, cutting down on headers may prove to be a very very insignificant of reducing traffic load.

That said, I am curious to know why you are setting the request headers to "." rather than an empty string "".

于 2009-03-02T12:47:29.847 回答
0

I would forgo this kind of micro-optimizations and look into a push model instead. By way of a starting place:

Both of these are typically paired with an XMPP server on the back-end.

于 2009-03-02T20:33:58.400 回答