强制Web服务器返回未压缩的数据(No gzip)

我正在使用http node.js 模块来发出http请求 。 我想强制Web服务器返回未压缩的数据。 [没有gzip,没有deflate]

请求标头

headers: { 'Accept-Encoding': 'gzip,deflate,sdch', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/31.0.1650.57 Chrome/31.0.1650.57 Safari/537.36', } 

我尝试使用这个'Accept-Encoding': '*;q=1,gzip=0'但没有运气。

我看到两种方法:

  1. 强制Web服务器返回未压缩的数据。
  2. 使用一些nodeJs模块解压缩压缩的数据

我想去#1。

如果您向外部服务器发送http请求,而您无法控制此请求,并且它不会对Accept-Encoding请求标头做出反应,那么您必须处理压缩的响应并稍后进行解压缩。 我build议你使用zlib模块。 这是一个例子:

 var zlib = require('zlib'); //... request.on('response', function(response){ var contentEncoding = response.headers['content-encoding']; response.on('data', function(data){ switch(contentEncoding){ case 'gzip': zlib.gunzip(data, function(error, body){ if(error){ //Handle error } else { //Handle decompressed response body } }); break; case 'deflate': zlib.inflate(data, function(error, body){ if(error){ //Handle error } else { //Handle decompressed response body } }); break; default: //Handle response body break; } }); });