nodejs+短信服务+网易云信

var http = require('http');  
var sha1=require('sha1');
var qs = require('querystring'); 
var Appkey='6185fa32a38c06ff0995a5fdb3efbbaa';
var Nonce="asd";
var CurTime=2221222;
var AppSecret='      ';
var str=AppSecret + Nonce + CurTime;
var CheckSum=sha1(str);

   
var post_data = {  
    templateid:3055220,
	mobiles:'["18071996467"]',
	params:'["2008"]'
};//这是需要提交的数据  
   
   
var content = qs.stringify(post_data); //序列化 a=c&name=d  并且将中文字符进行urlencode 编码进行传输
   console.log(content)
var options = {  
    hostname: 'api.netease.im',  
	//port:443,
    path: '/sms/sendtemplate.action',  
    method: 'POST',  
    headers: {  
		'AppKey'		: Appkey,
		'Nonce'			: Nonce,
		'CurTime'		: CurTime,
		'CheckSum'		: CheckSum,
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
			
    }  
};  
   
var req = http.request(options, function (res) {  
    console.log('STATUS: ' + res.statusCode);  
    //console.log('HEADERS: '+res.headers);  
    res.setEncoding('utf8');  
    res.on('data', function (chunk) {  
        console.log('BODY: ' + chunk);  
    });  
});  
   
req.on('error', function (e) {  
    console.log('problem with request: ' + e.message);  
});  
   
// write data to request body  
req.write(content);  
   
req.end();

网易云信地址:http://netease.im/?from=bdjjnim0035


nodejs方法2 request模块

var request = require('request');
var sha1=require('sha1');
var qs = require('querystring');
var Appkey='6185fa32a38c06ff0995a5fdb3efbbaa';
let now=Date.now();
let CurTime=parseInt(now/1000)+""; //当前时间秒数
var Nonce=sha1(CurTime);  //随机数
var AppSecret='    ';
var str=AppSecret + Nonce + CurTime;
var CheckSum=sha1(str);
var post_data = {
    templateid:3055220,
    mobiles:'["18071996467"]',
    params:'["我是lwei"]'
};//这是需要提交的数据
var content = qs.stringify(post_data);


var options = {
    url: 'https://api.netease.im/sms/sendtemplate.action?'+content, //拼接也是在body 不拼接就写body:"", 封装好的
    method: 'POST',
    headers: {
        'AppKey'        : Appkey,
        'Nonce'         : Nonce,
        'CurTime'       : CurTime,
        'CheckSum'      : CheckSum,
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',

    }
};


function callback(error, response, body) {
    console.log(error)
    console.log(response);
    // if (!error && response.statusCode == 200) {
    //     var info = JSON.parse(body);
    //     console.log(info.stargazers_count + " Stars");
    //     console.log(info.forks_count + " Forks");
    // }
    //var info = JSON.parse(body);
    console.log(body);


}

request(options, callback);



你可能感兴趣的:(nodejs)