简体   繁体   English

从XMLHttpRequest删除HTTP标头

[英]Removing HTTP headers from an XMLHttpRequest

I am working on an ajax long polling type application, and I would like to minimize the amount of bandwidth I am using. 我正在研究ajax长轮询类型的应用程序,并且我想最小化我正在使用的带宽量。 One of the big costs right now are the client side HTTP headers. 当前的一大成本是客户端HTTP标头。 Once I have a connection established and a session id stored on the client, I don't really want to squander any more bandwidth transferring redundant http information (such as browser type, accept encodings, etc.). 建立连接并将会话ID存储在客户端后,我真的不想浪费更多的带宽来传输冗余的HTTP信息(例如浏览器类型,接受编码等)。 Over the course of many connections, this quickly adds up to a lot of data! 在许多连接过程中,这很快会增加大量数据!

I would really like to just take my XMLHttpRequest and nuke all of the headers so that only the absolute minimum gets transmitted to the server. 我真的很想接受我的XMLHttpRequest并核对所有标头,以便仅将绝对最小值发送到服务器。 Is it possible to do this? 是否有可能做到这一点?

You have very little control over request headers, but you can still do a few things - 您对请求标头的控制很少,但是您仍然可以做一些事情-

  1. Reduce the size of the cookie. 减少cookie的大小。 In general, you only want the session id, everything else can be eliminated and stored server side. 通常,您只需要会话ID,其他所有内容都可以消除并存储在服务器端。
  2. Minimize http referrer by keeping a short URL. 通过保留简短的URL来最小化http Referrer。 The longer your page url, the more data will have to be sent via the http referrer. 页面网址越长,必须通过http引荐来源网址发送的数据就越多。 One trick is to store data in the fragment identifier (the portion of the url after the #). 一种技巧是将数据存储在片段标识符(#后面的url部分)中。 The fragment identifier is never sent to the server, so you save a few bytes over there. 片段标识符永远不会发送到服务器,因此您可以在那儿保存一些字节。
  3. Some request headers are only sent if you had previous set corresponding response headers. 仅当您先前设置了相应的响应头时,才发送某些请求头。 For example, you can indirectly control the ETag and if-modified-since request headers . 例如,您可以间接控制ETag和if-modified-since请求标头

You may want to consider Web Sockets . 您可能需要考虑使用Web套接字 Support is pretty good (IE10+). 支持非常好(IE10 +)。

您可以在发送请求之前使用setRequestHeader()覆盖某些标准标头,但是浏览器可能不允许覆盖某些标头,而且似乎无法获取标头列表(除了询问服务器外)将它们回显给您),以了解要覆盖的内容。

I think it's possible to remove all headers at least in some browsers. 我认为至少在某些浏览器中可以删除所有标头。 Take a look at the communication between gmail/calendar apps and the backend from google in chrome (it's not the same in firefox) it's possible google has some hidden api for the XMLHttpRequest object, you'll see something like the below output (notice there is no request headers section): 看看gmail / calendar应用程序和chrome中google的后端之间的通信(在firefox中是不一样的),这可能是google为XMLHttpRequest对象提供了一些隐藏的api,您会看到类似下面的输出(注意那里没有请求标头部分):

Request URL:https://mail.google.com/mail/u/0/channel/bind?XXXXXXXXXXXXXX
Request Method:POST
Status Code:200 OK

Query String Parameters
OSID:XXXXXXXXXXXXX
OAID:XXXXXXXXX
VER:8
at:XXXXXXXXXXXXXX
it:30
SID:XXXXXXXXXXXX
RID:XXXXXXXXX
AID:XXXXXXXXXX
zx:XXXXXXXXXXXX
t:1

Request Payload
count=1&ofs=211&req0_type=cf&req0_focused=1&req0__sc=c

Response Headers
cache-control:no-cache, no-store, max-age=0, must-revalidate
content-encoding:gzip
content-type:text/plain; charset=utf-8
date:Tue, 09 Oct 2012 08:52:46 GMT
expires:Fri, 01 Jan 1990 00:00:00 GMT
pragma:no-cache
server:GSE
status:200 OK
version:HTTP/1.1
x-content-type-options:nosniff
x-xss-protection:1; mode=block

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM