博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Node.Js http模块(一)-发送http请求实例
阅读量:4288 次
发布时间:2019-05-27

本文共 3275 字,大约阅读时间需要 10 分钟。

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实例:注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();
参数说明:

options can be an object or a string. If options is a string, it is automatically parsed with .

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  hostname is preferred over host.
  • family: IP address family to use when resolving host and hostname. Valid values are 4 or 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  behavior. When an Agent is used request will default to Connection: keep-alive. Possible values:
    • undefined (default): use  for this host and port.
    • Agent object: explicitly use the passed in Agent.
    • false: opts out of connection pooling with an Agent, defaults request to Connection: close.
更多:

转载地址:http://cyogi.baihongyu.com/

你可能感兴趣的文章
【Java并发编程】一.并发简介
查看>>
【Java并发编程】二.Java并发基础
查看>>
Java中的Unsafe类
查看>>
分享一次解决线上java应用导致JVM内存溢出(OOM)的问题
查看>>
RabbitMQ基础概念介绍
查看>>
在虚拟机上安装RabbitMQ
查看>>
ConcurrentHashMap图例
查看>>
轻量级数据库访问框架FastSQL
查看>>
mysql常用函数-使用总结
查看>>
ElasticSearch基础概念
查看>>
Redis雪崩、穿透、热点key等优化
查看>>
redis基础知识总结
查看>>
智慧园区三维可视化物联网运营管理平台
查看>>
智慧社区网格化服务管理信息平台
查看>>
智慧消防三维预案辅助决策系统
查看>>
基于3DGIS+BIM的智慧园区运维管理平台
查看>>
时空位置大数据AI平台技术实现架构设计
查看>>
智慧社区GIS系统开发详细设计
查看>>
智慧园区导航可视化分析平台技术方案
查看>>
智慧停车场综合解决方案
查看>>