/*
* post请求方法(推荐)
* objUrl obj 主机名,路径
* objParam obj 附加数据对象
*/
function requestPost(objUrl, objParam)
{
var http = require('http'); //载入https模块
var qs = require('querystring'); //载入Query String模块
var fs = require('fs'); //载入fs模块读取文件
var content = qs.stringify(objParam); //url编码参数
var options = {
hostname: objUrl.hostname,
port: 80,
path: objUrl.path,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
};
var reqCallBack = function (res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) { //有数据时读数据
console.log('POST返回结果: ' + chunk);
});
};
var req = http.request(options, reqCallBack);
req.write(content); //POST方法传输数据
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
req.end();
}
/*
* get请求方法
* objUrl obj 主机名,路径
* objParam obj 附加数据对象
*/
function requestGet(objUrl, objParam)
{
var http = require('http'); //载入http模块
var qs = require('querystring'); //载入Query String模块
var fs = require('fs'); //载入fs模块读取文件
var content = qs.stringify(objParam); //url编码参数
var options = {
hostname: objUrl.hostname,
port: 80,
path: objUrl.path + '?' + content,
method: 'GET'
};
var reqCallBack = function (res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) { //有数据时读数据
console.log('GET返回结果: ' + chunk);
});
};
var req = http.request(options, reqCallBack);
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
req.end();
}
/*
*
* @requestType string 请求方式,POST或者GET,推荐POST
*/
function getContent(requestType)
{
var objUrl = {
hostname: 'v.1dq.com', //主机名
path: '/api/v4/Organizationcode2', //api路径
}
var objParam = {
'key' : '您申请的key,在会员中心->我的数据->对应数据的下方',
'entName' : '',
}
if(requestType == 'GET'){
requestGet(objUrl,objParam);
}else{
requestPost(objUrl,objParam);
}
}
console.log('Hello,www.APIStore.cn');
getContent('POST');