nodejs -- http模块. request() 方法 , get方法.

时间:2025-01-08 13:36:08

1. request方法:

提交评论到慕课网:

 var http = require('http');
var querystring = require('querystring'); var postData = querystring.stringify({
'content': '很喜欢Scot老师的课程. 希望尽快学会nodejs!',
'cid': 348
}); var options = {
hostname: 'www.imooc.com',
port: 80,
path: '/course/docomment',
method: 'POST',
headers: {
'Accept':'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding':'gzip, deflate',
'Accept-Language':'zh-CN,zh;q=0.8',
'Connection':'keep-alive',
'Content-Length':postData.length,
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
'Cookie':'PHPSESSID=lvk77ha5lgp411jqbl2n9dovd0; imooc_uuid=94406a08-7461-473e-8676-a730e6d1f16a; imooc_isnew=1; imooc_isnew_ct=1501826723; loginstate=1; apsid=E0M2ExNjBmNWQwN2Q0MDc0YzhhZWUwMmY4MTg3NmEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMTM1NTg1NAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB4X2RhY2hlbmdAMTI2LmNvbQAAAAAAAAAAAAAAAAAAAGY3YzYwNzdjYjlmMjBkYTA0OWI0OWE3ZjhiZDgxMzEz6Q6EWekOhFk%3DNm; last_login_username=x_dacheng%40126.com; Hm_lvt_f0cfcccd7b1393990c78efdeebff3968=1501826670; Hm_lpvt_f0cfcccd7b1393990c78efdeebff3968=1501826738; IMCDNS=0; cvde=59840ea3a723c-7',
'Host':'www.imooc.com',
'Origin':'http://www.imooc.com',
'Referer':'http://www.imooc.com/comment/348',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36',
'X-Requested-With':'XMLHttpRequest'
}
}; //发送请求:
var req = http.request(options, function(res){ console.log('status: ' + res.statusCode);
console.log('headers: ' + JSON.stringify(res.headers)); res.on('data', function(chunk){
console.log(Buffer.isBuffer(chunk));
console.log(typeof chunk);
}); res.on('end', function(){
console.log('评论完毕!');
});
}); req.on('error', function(e){
console.log('Error: ' + e.message);
}); //写入请求数据:
req.write(postData); //结束请求:必须写的:
req.end();

nodejs -- http模块.   request() 方法 ,   get方法.

注意:

  第56行的代码:   req.end(); 必须写.

------------------

2. get() 方法.

get方法是 对 request方法的封装,    get方法 自带 req.end()

nodejs -- http模块.   request() 方法 ,   get方法.

nodejs 请求端: get.js

 var http = require('http');
var querystring = require('querystring'); var postData = {
'name': '小明',
'age': 26
}; var postDataStr = querystring.stringify(postData); http.get('http://www.a.com/response.php?'+postDataStr, function(res){
console.log('status: ' + res.statusCode);
console.log('headers: ' + JSON.stringify(res.headers));
var txt = '';
res.on('data', function(chunk){
// console.log(Buffer.isBuffer(chunk));
// console.log(typeof chunk); txt += chunk;
}); res.on('end', function(){
console.log(typeof txt);
console.log(txt); 29 console.log(typeof JSON.parse(txt));
30 console.log(JSON.parse(txt));
31 console.log(JSON.parse(txt).name);
32 console.log(querystring.unescape(JSON.parse(txt).name));
});
}).on('error', function(e){
console.log('Error: '+e.message);
});

PHP接收端: response.php

 <?php

     $name = $_GET["name"] . '--返回数据';
$age = $_GET['age'] + 100; echo json_encode(array(
'name' => $name,
'age' => $age,
'address' => 'beijing'
)); ?>

运行:

nodejs -- http模块.   request() 方法 ,   get方法.

 注意:

  •   返回的数据 是 json格式的字符串 ,因此要使用 JSON.parse() 转化为json对象.

  

参考链接: