admin管理员组文章数量:1323714
I am working on an ajax long polling type application, and I would like to minimize the amount of bandwidth I am using. One of the big costs right now are the client side HTTP headers. 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.). 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. Is it possible to do this?
I am working on an ajax long polling type application, and I would like to minimize the amount of bandwidth I am using. One of the big costs right now are the client side HTTP headers. 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.). 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. Is it possible to do this?
Share Improve this question asked Mar 1, 2011 at 19:11 MikolaMikola 9,3262 gold badges36 silver badges42 bronze badges 5- It's not possible. The headers are not parsed by Javascript, they are sent by the browser when the connection for your request is made. – Cfreak Commented Mar 1, 2011 at 19:18
- HTTP header contains operational parameters, most of which define the very request; you might be able to limit to absolute minimum but will not be able to get rid of them all – Kris Ivanov Commented Mar 1, 2011 at 19:18
- @Ivanov, that is true. However there are many parameters that are just wasteful. For example, if I already have a session established, I don't need to know the user-agent, accept-encoding, language, charset, host or referer. All of this data could be safely dropped from the header since my server is never going to use it anyway. And it adds up to a lot of extra space too! Almost half a kilobyte per packet! I am just offended that it is there and would be much happier if I could just send a raw post + content length packet to the server. – Mikola Commented Mar 1, 2011 at 19:55
- 1 HTTP is meant to be stateless. Every request has to have plete information, otherwise you break the spirit of HTTP. Browsers are creating a new protocol ("Web Sockets") for your use case, but that will take a while to stabilize. – Sripathi Krishnan Commented Mar 1, 2011 at 20:07
- 3 The XmlHttpRequest spec includes a whole set of headers that are not allowed to be overridden by the user, including User-Agent and Accept-*: w3/TR/XMLHttpRequest/#the-setrequestheader-method – monsur Commented Mar 2, 2011 at 1:50
3 Answers
Reset to default 3You have very little control over request headers, but you can still do a few things -
- Reduce the size of the cookie. In general, you only want the session id, everything else can be eliminated and stored server side.
- Minimize http referrer by keeping a short URL. The longer your page url, the more data will have to be sent via the http referrer. One trick is to store data in the fragment identifier (the portion of the url after the #). The fragment identifier is never sent to the server, so you save a few bytes over there.
- 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.
You may want to consider Web Sockets. Support is pretty good (IE10+).
You may be able to override some of the standard headers using setRequestHeader()
before sending the request, but it is possible the browser may not allow overriding of some and it seems there is no way to get a list of headers (besides asking the server to echo them back to you) to know which to try to override.
I think it's possible to remove all headers at least in some browsers. Take a look at the munication 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):
Request URL:https://mail.google./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
本文标签: javascriptRemoving HTTP headers from an XMLHttpRequestStack Overflow
版权声明:本文标题:javascript - Removing HTTP headers from an XMLHttpRequest - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742126895a2421982.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论