NodeJS创建http client
1 var http = require('http');
2
3 var data = JSON.stringify({ 'important': 'data' });
4 var cookie = 'something=anything'
5
6 var client = http.createClient(80, 'www.example.com');
7
8 var headers = {
9 'Host': 'www.example.com',
10 'Cookie': cookie,
11 'Content-Type': 'application/json',
12 'Content-Length': Buffer.byteLength(data,'utf8')
13 };
14
15 var request = client.request('POST', '/', headers);
16
17 // listening to the response is optional, I suppose
18 request.on('response', function(response) {
19 response.on('data', function(chunk) {
20 // do what you do
21 });
22 response.on('end', function() {
23 // do what you do
24 });
25 });
26 // you'd also want to listen for errors in production
27
28 request.write(data);
29
30 request.end();
2
3 var data = JSON.stringify({ 'important': 'data' });
4 var cookie = 'something=anything'
5
6 var client = http.createClient(80, 'www.example.com');
7
8 var headers = {
9 'Host': 'www.example.com',
10 'Cookie': cookie,
11 'Content-Type': 'application/json',
12 'Content-Length': Buffer.byteLength(data,'utf8')
13 };
14
15 var request = client.request('POST', '/', headers);
16
17 // listening to the response is optional, I suppose
18 request.on('response', function(response) {
19 response.on('data', function(chunk) {
20 // do what you do
21 });
22 response.on('end', function() {
23 // do what you do
24 });
25 });
26 // you'd also want to listen for errors in production
27
28 request.write(data);
29
30 request.end();