nodejs封装的webget webpost方法

在我之前的项目中,经常用到Nodejs通过post\get方法访问其它网站、webapi。下面是我封装的

Get、Post方法,很适合在一些web字符串收发场景使用(暂不支持文件、二进制流等传输)。

****************************************************************************************************

 

var util =require('util');

var http =require("http");

varquerystring = require('querystring');

var iconv =require('iconv-lite');

 

//web get

exports.WebGet = function (url, callback,encode) {

   try {

       http.get(url, function (req,res) {

            var chunks = [];

            var len = 0;

 

            req.on('data', function (chunk) {

                chunks.push(chunk);

                len += chunk.length;

            });

            req.on('end', function () {

                var data = Buffer.concat(chunks, len);

 

                var html;

                if (encode != null) {

                    html = iconv.decode(data,encode);

                } else {

                    html = data.toString();

                }

 

                callback != null ? callback(html) : null;

            });

       });

   }

   catch (e) {

       callback != null ?callback(e.message) : null;

   }

}

 

//web post

exports.WebPost = function (_hostname, _port,_path,_data,callback) {

   try {

       varpost_data = querystring.stringify(_data);

 

       var option= {

            hostname: _hostname,

            port: _port,

            path: _path,

            method: 'POST',

            headers: {

                "Content-Type": 'application/x-www-form-urlencoded',

                "Content-Length": post_data.length

            }

       };

 

       var req =http.request(option, function (res) {

                var html = "";

                res.setEncoding('utf8');

                res.on("data", function (chunk) {

                    html += chunk;

                });

 

                res.on('end', function () {

                    callback != null ? callback(html) : null;

                });

       }).on("error", function (e) {

            callback != null ?callback(e.message) : null;

       });

       req.write(post_data + "\n");

       req.end();

   }

   catch (e) {

       callback != null ?callback(e.message) : null;

   }

};

 

****************************************************************************************************

//WebGet使用实例:

_web.WebGet("http://www.xxx.com/a/b.htm", callBackFun, 'gb2312');

 

//WebPost使用实例:

var data ={

   id: 1,

   msg: 'abc'

};

_web.WebPost("www.xxx.com", 80, "/a/SendMsg", data, callBackFun);

 

****************************************************************************************************

//注1:nodejs默认支持utf-8编码,需要进行编码转换的请安装"iconv-lite"模块:npm install iconv-lite;

//注2:WebGet没有使用http.request中的method: 'GET', 而是使用 http.get。这是为了多展示一个方法,其效果是一样的;

//注3:WebPost网址参数不用加"http://",内部也没有像WebGet里一样在res.on("data")、res.on('end')里做chuank的buffer、string处理,有需要的请自行复制处理。

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值