Node.Js http模块(一)-发送http请求实例
来源:互联网 发布:苹果cms有自带采集插件 编辑:程序博客网 时间:2024/05/20 16:00
Node.Js http模块可以创建服务器应用实例,也能发送http请求
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实例:
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', header:{ //'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();参数说明:
options
can be an object or a string. If options
is 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 forhost
. To supporturl.parse()
hostname
is preferred overhost
.family
: IP address family to use when resolvinghost
andhostname
. Valid values are4
or6
. 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
: ControlsAgent
behavior. When an Agent is used request will default toConnection: keep-alive
. Possible values:undefined
(default): usehttp.globalAgent
for this host and port.Agent
object: explicitly use the passed inAgent
.false
: opts out of connection pooling with an Agent, defaults request toConnection: close
.
0 0
- Node.Js http模块(一)-发送http请求实例
- Node.js发送HTTP请求
- node js 发送http请求
- node.js发送http(get,post)请求
- node.js中使用http模块发送请求时的一个小错误
- Node.js HTTP模块
- Node.js中http 接收get请求、接收post请求、发送get请求、发送post请求
- http请求利器---node.js中的request模块
- Node.js输出HTTP请求
- Node.js 的 http 模块
- Node.js中的HTTP模块
- node.js的http模块
- Node.js基本模块http
- Android 发送http请求实例
- http发送服务请求实例
- java发送http请求实例
- 第一个 node js http 发送post请求(测试无误)
- c socket 发送http请求 (一)
- AngularJS初探
- KMP
- C#之递推问题
- java concurrent 包中的Sync类
- PLSQL中的&字符处理
- Node.Js http模块(一)-发送http请求实例
- Linq To DataSet
- python python-eggs 异常解决方法
- Android 事件分发机制 和 消息处理机制
- C 语言 undefined reference to 'sqrt' 问题解决
- Android 自定义符合软件整体风格的dialog
- Swift 初始化顺序
- 聚集索引和非聚集索引(整理)
- Java线程(二)线程池