-
Notifications
You must be signed in to change notification settings - Fork 2
/
httpDataCollector.js
61 lines (48 loc) · 1.29 KB
/
httpDataCollector.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
'use strict';
const http = require('http');
class httpDataCollector {
constructor(hostname, port, path, username, password) {
this.hostname = hostname;
this.port = port;
this.path = path;
this.username = username;
this.password = password;
}
collect(cookie, resp_callback) {
var html_response = '';
var agent = new http.Agent();
var options = {
hostname: this.hostname,
port: this.port,
path: this.path,
method: 'GET',
agent: agent,
headers: {
'Cookie': cookie,
'Connection': 'keep-alive'
}
};
// show response from server
var request = http.request(options, function(response) {
response.setEncoding('utf8');
response.on('data', function (chunk) {
html_response = html_response + chunk;
});
response.on('end', function () {
//console.log(html_response);
resp_callback(html_response);
});
});
// Error handling (e.g. host not reachable)
request.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// (s)end the request
request.end();
// debug: agent information
//console.dir(agent);
return html_response;
}
};
module.exports = httpDataCollector;
// vim: ts=2 sw=2 sts=2 et