Node.Js http模块(一)-发送http请求实例
2016-08-31 10:21
846 查看
Node.Js http模块可以创建服务器应用实例,也能发送http请求
1.http.get(options[, callback])
发送简单Get请求,并响应
发送Post实例:注http请求头使用headers指定
a string, it is automatically parsed with
Options:
Defaults to
of the server to issue the request to. Defaults to
To support
preferred over
use when resolving
Valid values are
When unspecified, both IP v4 and v6 will be used.
Defaults to 80.
to bind for network connections.
(use one of host:port or socketPath).
the HTTP request method. Defaults to
to
An exception is thrown when the request path contains illegal characters. Currently, only spaces are rejected but that may change in the future.
request headers.
i.e.
When an Agent is used request will default to
Possible values:
this host and port.
the passed in
pooling with an Agent, defaults request to
更多:
NodeJs开发环境搭建之Visual Studio Code
NodeJS命令找不到:'express' 不是内部或外部命令,也不是可运行的程序或批处理文件。
NodeJs的安装和环境变量配置
1.http.get(options[, callback])
发送简单Get请求,并响应
var http=require('http'); //get 请求外网 http.get('http://www.gongjuji.net',function(req,res){ var html=''; req.on('data',function(data){ html+=data; }); req.on('end',function(){ console.info(html); }); });2.http.request(options[, callback]) // 使用详细配置,发送Get或Post请求
发送Post实例:注http请求头使用headers指定
var http=require('http'); var querystring=require('querystring'); //发送 http Post 请求 var postData=querystring.stringify({ msg:'中文内容' }); var options={ hostname:'www.gongjuji.net', port:80, path:'/', method:'POST', headers:{ //'Content-Type':'application/x-www-form-urlencoded', 'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8', 'Content-Length':Buffer.byteLength(postData) } } var req=http.request(options, function(res) { console.log('Status:',res.statusCode); console.log('headers:',JSON.stringify(res.headers)); res.setEncoding('utf-8'); res.on('data',function(chun){ console.log('body分隔线---------------------------------\r\n'); console.info(chun); }); res.on('end',function(){ console.log('No more data in response.********'); }); }); req.on('error',function(err){ console.error(err); }); req.write(postData); req.end();发送Get请求实例:
//发送Get请求 var http=require('http'); var querystring=require('querystring'); var data={ age:13, time:new Date().getTime() }; var content=querystring.stringify(data); var options={ hostname:'www.gongjuji.net', port:80, path:'/', method:'GET' } //创建请求 var req=http.request(options,function(res){ console.log('STATUS:'+res.statusCode); console.log('HEADERS:'+JSON.stringify(res.headers)); res.setEncoding('utf-8'); res.on('data',function(chunk){ console.log('数据片段分隔-----------------------\r\n'); console.log(chunk); }); res.on('end',function(){ console.log('响应结束********'); }); }); req.on('error',function(err){ console.error(err); }); req.end();参数说明:
optionscan be an object or a string. If
optionsis
a string, it is automatically parsed with
url.parse().
Options:
protocol: Protocol to use.
Defaults to
'http:'.
host: A domain name or IP address
of the server to issue the request to. Defaults to
'localhost'.
hostname: Alias for
host.
To support
url.parse()
hostnameis
preferred over
host.
family: IP address family to
use when resolving
hostand
hostname.
Valid values are
4or
6.
When unspecified, both IP v4 and v6 will be used.
port: Port of remote server.
Defaults to 80.
localAddress: Local interface
to bind for network connections.
socketPath: Unix Domain Socket
(use one of host:port or socketPath).
method: A string specifying
the HTTP request method. Defaults to
'GET'.
path: Request path. Defaults
to
'/'. Should include query string if any. E.G.
'/index.html?page=12'.
An exception is thrown when the request path contains illegal characters. Currently, only spaces are rejected but that may change in the future.
headers: An object containing
request headers.
auth: Basic authentication
i.e.
'user:password'to compute an Authorization header.
agent: Controls
Agentbehavior.
When an Agent is used request will default to
Connection: keep-alive.
Possible values:
undefined(default): use
http.globalAgentfor
this host and port.
Agentobject: explicitly use
the passed in
Agent.
false: opts out of connection
pooling with an Agent, defaults request to
Connection: close.
更多:
NodeJs开发环境搭建之Visual Studio Code
NodeJS命令找不到:'express' 不是内部或外部命令,也不是可运行的程序或批处理文件。
NodeJs的安装和环境变量配置
相关文章推荐
- node.js中使用http模块发送请求时的一个小错误
- 详解node.js的http模块实例演示
- node.js 核心http模块,起一个服务器,返回一个页面的实例
- JAVA发送HTTP请求,返回HTTP响应内容,实例及应用
- 详解Node.js API系列 Http模块(2) CNodejs爬虫实现
- Nodejs HTTP请求的超时处理 Nodejs HTTP Client Request Timeout Handle
- Node.js 的 http 模块
- 【JS】【客户端】脚本化HTTP —— 借助<script>发送HTTP请求:JSONP
- JAVA发送HTTP请求,返回HTTP响应内容,实例及应用
- node.js使用nodemailer发送邮件实例
- Node.js中HTTP模块与事件模块详解
- 从零开始学习Node.js例子七 发送HTTP客户端请求并显示响应结果
- node.js调用C++开发的模块实例
- python编程_python通过get方式,post方式发送http请求和接收http响应_import urllib模块,import urllib2模块,import httplib模块
- node.js--HTTP模块
- 详解Node.js API系列 Http模块(1) 构造一个简单的静态页服务器
- JAVA发送HTTP请求,返回HTTP响应内容,实例及应用
- Node.js学习笔记(3、http模块)
- nodejs http 发送请求
- Node.js中HTTP模块与事件模块详解