-
Notifications
You must be signed in to change notification settings - Fork 2
/
httpDataWriter.js
72 lines (56 loc) · 1.56 KB
/
httpDataWriter.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
65
66
67
68
69
70
71
72
'use strict';
const http = require('http');
class httpDataWriter {
constructor(hostname, port, username, password) {
this.hostname = hostname;
this.port = port;
this.path = "/save.php";
this.username = username;
this.password = password;
}
setValue(cookie, key, value, resp_callback) {
var html_response = '';
var agent = new http.Agent();
var thz_option = [{
"name": key,
"value": value
}]
var post_data = 'data='+ JSON.stringify(thz_option);
var options = {
hostname: this.hostname,
port: this.port,
path: this.path,
method: 'POST',
agent: agent,
headers: {
'Cookie': cookie,
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(post_data)
}
};
// 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);
});
//Write our post data to the request
request.write(post_data);
// (s)end the request
request.end();
// debug: agent information
//console.dir(agent);
return html_response;
}
};
module.exports = httpDataWriter;
// vim: ts=2 sw=2 sts=2 et