nodejs -- http模块. request() 方法 , get方法.
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();
注意:
第56行的代码: req.end(); 必须写.
------------------
2. get() 方法.
get方法是 对 request方法的封装, get方法 自带 req.end()
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'
)); ?>
运行:
注意:
- 返回的数据 是 json格式的字符串 ,因此要使用 JSON.parse() 转化为json对象.
参考链接:
nodejs -- http模块. request() 方法 , get方法.的更多相关文章
- Node.js process 模块常用属性和方法
Node.js是常用的Javascript运行环境,本文和大家发分享的主要是Node.js中process 模块的常用属性和方法,希望通过本文的分享,对大家学习Node.js http://www.m ...
- python3 中mlpy模块安装 出现 failed with error code 1的决绝办法(其他模块也可用本方法)
在python3 中安装其它模块时经常出现 failed with error code 1等状况,使的安装无法进行.而解决这个问题又非常麻烦. 接下来以mlpy为例,介绍一种解决此类安装问题的办法. ...
- Node.js -- Router模块中有一个param方法
这段时间一直有在看Express框架的API,最近刚看到Router,以下是我认为需要注意的地方: Router模块中有一个param方法,刚开始看得有点模糊,官网大概是这么描述的: 1 Map lo ...
- request对象多种方法封装表单数据
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, ...
- Struts2获取request三种方法
Struts2获取request三种方法 struts2里面有三种方法可以获取request,最好使用ServletRequestAware接口通过IOC机制注入Request对象. 在Actio ...
- python之序列化模块、双下方法(dict call new del len eq hash)和单例模式
摘要:__new__ __del__ __call__ __len__ __eq__ __hash__ import json 序列化模块 import pickle 序列化模块 补充: 现在我们都应 ...
- request对象的方法及其参数的传递
先设计一个简单的登录界面index.htm: <html><head><title>request的使用</title></head>< ...
- python inspect 模块 和 types 模块 判断是否是方法,模块,函数等内置特殊属性
python inspect 模块 和 types 模块 判断是否是方法,模块,函数等内置特殊属性 inspect import inspect def fun(): pass inspect.ism ...
- os、os.path模块(文件/目录方法)
1.模块的概念:模块是一个包含所有定义的变量.函数的文件,模块可以被其余模块调用. 2.利用OS模块实现对系统文件的. os模块中常见的方法: gercwd() 返回当前工作目录 chdir( ...
随机推荐
- Shellcode入门
Shellcode入门 一.shellcode基础知识 Shellcode实际是一段代码(也可以是填充数据),是用来发送到服务器利用特定漏洞的代码,一般可以获取权限.另外,Shellcode一般是作为 ...
- ORA-12801/ORA-12853: insufficient memory for PX buffers: current 274880K, max needed 19722240K/ORA-04031解决方法
近日,现场一台服务器在运行时出现下列异常: ORA-12801: error signaled in parallel query server P139 ORA-12853: insufficien ...
- shell批量修改mysql用户密码
需求 现在有这么一个需求, 需要大批量修改用户的密码, 需要注意的规则是: 必须添加的字符: *$#sjdKw% 用户名的第一位+*$#sjdKw%+用户名的最后一位,比如用户名chenglee,密码 ...
- topcoder srm 560 div1
problem1 link 从大到小贪心,较大的数字应该放置在较浅的位置. problem2 link 最后的位置要么都是整数(经过偶数次变换),要么是$(p.5, q.5)$这种位置(奇数次变换). ...
- 【问题解决:信息提示】SpringBoot启动时提示The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path
问题描述 springboot程序在启动时提示信息 [2018-10-24 21:59:05.214] - 440 信息 [restartedMain] --- org.apache.catalina ...
- C# 控件置于最顶层、最底层
btn.BringToFront();//将控件放置所有控件最前端 btn.SendToBack();//将控件放置所有控件最底端
- ( 转) Awesome Image Captioning
Awesome Image Captioning 2018-12-03 19:19:56 From: https://github.com/zhjohnchan/awesome-image-capti ...
- Learning-Python【19】:Python常用模块(2)—— os、sys、shutil
os模块:与操作系统相关的模块 import os # 获取当前的工作目录 print(os.getcwd()) # 切换工作目录 os.chdir(r'E:\Python\test') print( ...
- Lintcode40-Implement Queue by Two Stacks-Medium
40. Implement Queue by Two Stacks As the title described, you should only use two stacks to implemen ...
- mac git 删除本地仓库文件
递归清除本地文件夹下的Git文件,如果想重新建立仓库,那么在重新初始化新建的git仓库 //删除文件夹下的所有 .git 文件 find . -name ".git" | xarg ...