码迷,mamicode.com
首页 > Web开发 > 详细

nodejs封装的webget webpost方法

时间:2018-02-05 12:31:01      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:code   参数   html   web   content   bapi   post   效果   path   

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

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

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

 

var util = require(‘util‘);

var http = require("http");

var querystring = 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 {

        var post_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处理,有需要的请自行复制处理。

 

nodejs封装的webget webpost方法

标签:code   参数   html   web   content   bapi   post   效果   path   

原文地址:https://www.cnblogs.com/luoc83/p/8416591.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!