如何为AJAX调用清除HTTP头?

时间:2023-01-09 20:16:12

I have developed a solution that relies on an AJAX call to retrieve information and update the client page every 10 seconds. This is working fine, but I am concerned at the scalability of the code, given the number and length of headers being passed from client to server and back again. I have removed a number of redundant headers on the server side, mostly ASP.NET-related, and now I'm attempting to cut down on the headers coming from the client.

我开发了一个依赖AJAX调用的解决方案,以每10秒检索信息并更新客户端页面。这是正常的,但是我关注代码的可伸缩性,考虑到从客户端传递到服务器的消息头的数量和长度。我已经在服务器端删除了一些冗余的头,主要是ASP。net相关的,现在我想要减少来自客户端的头信息。

The browser used by my company is IE (version 6, to be upgraded to 7 soon). This is an approximation of my current code:

我公司使用的浏览器是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();

Although I've read that it's possible to clear some of these headers, I haven't found a way of doing it that works in IE6. Setting them to null results in a Type Mismatch exception, so I've ended up just replacing them with '.' for the time being. Is there another way of clearing them or an alternative method of reducing the submitted HTTP headers?

虽然我已经读到可以清除其中的一些标题,但我还没有找到在IE6中工作的方法。将它们设置为null会导致类型不匹配异常,所以最后我只是用'替换它们。”“暂时。是否有其他方法清除它们或减少提交的HTTP头的替代方法?

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

此外,似乎根本没有办法替换或缩短“Referrer”头。

3 个解决方案

#1


3  

According to the WD spec

根据WD规范

The setRequestHeader() method appends a value if the HTTP header given as argument is already part of the list of request headers.

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

That is, you could only add headers, not replace them.

也就是说,您只能添加头文件,而不能替换它们。

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.

似乎根本没有办法替换或缩短“Referrer”头。

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

考虑到有些人错误地将“Referer”(原文如此)用作访问控制机制,这一点并不让我感到意外。

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.

您可以尝试确保当前页面URL不是太长,但说实话,我觉得所有这些都是过早优化的味道。无论您做什么,您的请求都将适用于一个IP包,因此gonig不存在明显的性能差异。

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.

Mibbit(正如您在博客中所提到的)尝试这些东西可能是值得的,因为Mibbit吸引了大量的流量,但是对于一个简单的全公司范围的应用程序,我认为跨浏览器和代理测试的负担是不值得的:混乱头的最终用户利益比。

#2


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.

IE 6和旧版本使用从MSXML创建的ActiveXObject。XMLHTTP(实际上派生自IXMLHTTPRequest),而IE 7和其他现代浏览器(如Mozilla)则使用称为XmlHttpRequest的内部对象。这可能就是为什么不能为MSXML实现将请求头设置为null,但可以为内置对象设置。

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.

因此,我不认为有任何方法可以集体清除所有的标头。您所提供的Mibbit的链接只提供了一个函数,将所有的标头设置为null。对于正常的场景,减少头信息对于减少流量负载来说是非常不重要的。

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

也就是说,我很好奇为什么要将请求头设置为“.”而不是空字符串。

#3


0  

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

我将放弃这种微优化,转而研究push模型。作为起点:

  • Flash can create persistent sockets, and broker the events into javascript.
  • Flash可以创建持久套接字,并将事件代理到javascript中。
  • You could implement Bidirectional-streams Over Synchronous HTTP (BOSH). See http://xmpp.org/extensions/xep-0124.html
  • 您可以通过同步HTTP (BOSH)实现双向流。见http://xmpp.org/extensions/xep - 0124. - html

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

这两者通常都与后端上的XMPP服务器配对。

#1


3  

According to the WD spec

根据WD规范

The setRequestHeader() method appends a value if the HTTP header given as argument is already part of the list of request headers.

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

That is, you could only add headers, not replace them.

也就是说,您只能添加头文件,而不能替换它们。

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.

似乎根本没有办法替换或缩短“Referrer”头。

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

考虑到有些人错误地将“Referer”(原文如此)用作访问控制机制,这一点并不让我感到意外。

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.

您可以尝试确保当前页面URL不是太长,但说实话,我觉得所有这些都是过早优化的味道。无论您做什么,您的请求都将适用于一个IP包,因此gonig不存在明显的性能差异。

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.

Mibbit(正如您在博客中所提到的)尝试这些东西可能是值得的,因为Mibbit吸引了大量的流量,但是对于一个简单的全公司范围的应用程序,我认为跨浏览器和代理测试的负担是不值得的:混乱头的最终用户利益比。

#2


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.

IE 6和旧版本使用从MSXML创建的ActiveXObject。XMLHTTP(实际上派生自IXMLHTTPRequest),而IE 7和其他现代浏览器(如Mozilla)则使用称为XmlHttpRequest的内部对象。这可能就是为什么不能为MSXML实现将请求头设置为null,但可以为内置对象设置。

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.

因此,我不认为有任何方法可以集体清除所有的标头。您所提供的Mibbit的链接只提供了一个函数,将所有的标头设置为null。对于正常的场景,减少头信息对于减少流量负载来说是非常不重要的。

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

也就是说,我很好奇为什么要将请求头设置为“.”而不是空字符串。

#3


0  

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

我将放弃这种微优化,转而研究push模型。作为起点:

  • Flash can create persistent sockets, and broker the events into javascript.
  • Flash可以创建持久套接字,并将事件代理到javascript中。
  • You could implement Bidirectional-streams Over Synchronous HTTP (BOSH). See http://xmpp.org/extensions/xep-0124.html
  • 您可以通过同步HTTP (BOSH)实现双向流。见http://xmpp.org/extensions/xep - 0124. - html

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

这两者通常都与后端上的XMPP服务器配对。