-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpManager.js
executable file
·64 lines (62 loc) · 1.99 KB
/
httpManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'use strict';
var zlib = require('zlib');
var request = require('request');
var util = require('util');
var GtConfig = require('./GtConfig');
var httpManager = {
/**
* HTTP POST请求,返回JSON数据格式
* @param host
* @param postData
* @param callback
* @return json数据
*/
post: function (host, postData, needGzip, callback) {
postData.version = GtConfig.getSDKVersion();
var tries = GtConfig.getHttpTryCount(); // 最大重连次数
attempt(host, tries);
function attempt(ho, times) {
var options = {
uri: ho,
method: 'post',
timeout: GtConfig.getHttpSoTimeOut(),
rejectUnauthorized: false,
headers: {
'Content-Type': 'text/html;charset=UTF-8',
'User-Agent': 'Getui nodejs',
'Accept': '*/*'
}
};
if (needGzip) {
options.gzip = true;
options.headers['Content-Encoding'] = 'gzip';
postData = zlib.gzipSync(JSON.stringify(postData));
} else {
options.json = true;
}
options.body = postData;
var action = postData['action'];
if (action != null && action.length > 0) {
options.headers['Gt-Action'] = action;
}
request(
options,
function (err, res, data) {
if (!err && res.statusCode == 200) {
//console.log("what? got res:" + util.inspect(data));
if (typeof data == 'string') {
data = JSON.parse(data);
}
//console.log("result:" + data.result);
callback && callback(null, data);
//console.log("callback over");
} else if (--times) {
attempt(host, times);
} else {
callback && callback(err, null);
}
});
}
}
};
module.exports = httpManager;