主要用于 zlib包
zlib.gzipSync(dataStr)
zlib.gunzip(option,cb)
request 传输 gizp 数据 需要设置headers
var headers = {'Accept-Encoding': 'gzip'};
response 获取 data 不能设置字符集
//res.setEncoding('utf8');绝壁不能加
不要用字符串拼接 用 数组arraylist.push
chunkStr
var req = http.request(options, function (res) {
res.setEncoding('utf8');
var dataBuf = '';
res.on('data', function (chunk) {
dataBuf += chunk;
});
res.on('end', function () {
callback(null, res.statusCode, dataBuf);
});
});
chunkArray
var req = http.request(options, function (res) {
// res.setEncoding('utf8');绝壁不能加这行
var dataList = [];
res.on('data', function (chunk) {
dataList.push(chunk);
});
res.on('end', function () {
var buffer = Buffer.concat(dataList);
zlib.gunzip(buffer, function (err, decoded) {
data = decoded.toString();
callback(null, res.statusCode, data);
});
});
});
Demo
var zlib = require('zlib');
var fs = require('fs');
var path = require('path');
var http = require('http');
var https = require('https');
var url = require('url');
var querystring = require('querystring');
var zlib = require('zlib');
var os = require('os');
var nodemailer = require('nodemailer');
// var tss = require("../../tss_sdk/tss");
var dnscache = require('dnscache')({
"enable" : true,
"ttl" : 300,
"cachesize" : 100
});
var MF = require('./core');
function HttpRequestGzip(hostname, port, path, headers, postData, callback) {
if (hostname.indexOf('http://') === 0) {
var urlObj = url.parse(hostname);
hostname = urlObj.hostname;
port = urlObj.port;
path = urlObj.path;
}
dnscache.lookup(hostname, function(err, result) {
if(err) {
MF.Log.formatError("Parse dns of %s failed.", hostname);
callback(new Error("Parse dns failed"));
return;
}
hostname = result;
onGetIp();
});
function onGetIp() {
var options = {};
options.hostname = hostname;
options.path = '/';
if (port)
options.port = port;
if (path)
options.path += path;
if (options.path)
options.path = options.path.replace('//', '/');
var postDataBuffer = null;
if (postData !== undefined && postData !== null) {
//gzip 压缩
postDataBuffer = MF.Zlib.Gzip(postData);
options.method = 'POST';
if (headers)
options.headers = headers;
else
options.headers = {};
if (!options.headers['Content-Type'])
options.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
options.headers['Content-Length'] = postDataBuffer.length;
} else {
options.method = 'GET';
if (headers)
options.headers = headers;
}
// MF.Log.logDebug('HttpRequest: ');
// MF.Log.logDebug(options);
var req = http.request(options, function (res) {
// res.setEncoding('utf8');绝壁不能加这行
var dataList = [];
res.on('data', function (chunk) {
dataList.push(chunk);
});
res.on('end', function () {
var buffer = Buffer.concat(dataList);
zlib.gunzip(buffer, function (err, decoded) {
data = decoded.toString();
callback(null, res.statusCode, data);
});
});
});
req.on('error', function (e) {
callback(e);
});
if (postDataBuffer !== undefined && postDataBuffer !== null) {
req.write(postDataBuffer );
}
req.end();
}
}
MF.Zlib
var MF = require('./core');
var zlib = require('zlib');
function Gzip(data) {
return zlib.gzipSync(JSON.stringify(data));
}
function Gunzip(data) {
zlib.gunzip(data,function (error,res) {
});
return 0;
}
MF.Zlib = {};
MF.Zlib.Gzip = Gzip;
MF.Zlib.Gunzip = Gunzip;
网友评论