接收get请求
/**
*接收一个带参数的http请求
* 127.0.0.1:3000/http_get?name=小小沉沉&password=qwer
* 一般会有两次请求 另一次为http://127.0.0.1:3000/favicon.ico 这个是浏览器自动发起的请求,需要特殊处理
*
*/
//导入http模块
var http=require('http');
var util = require('util');
//导入url模块
var url=require('url');
http.createServer(function(request,response){
response.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});//设置response编码为utf-8
//接收浏览器url
var url_Obj=url.parse(request.url);
//请求参数中有中文是推介,会自动处理中文问题,推介使用
var url_Obj_Json=url.parse(request.url,true);
console.log(url_Obj_Json);
var url_Obj_Json_str=JSON.stringify(url_Obj_Json.query);
console.log(url_Obj_Json_str);
//解决http://127.0.0.1:3000/favicon.ico 引起的重复请求
if(url_Obj_Json.path != "/favicon.ico"){
response.write(util.inspect(url.parse(request.url, true)));
}
response.write("=======================================");
response.end();
}).listen(3000);
console.log("--HTTP NodeJS Connect--");
接收post请求
/**
*接收一个带参数的http请求
* 127.0.0.1:3000/http_get
* name=小小沉沉&password=qwer
*/
//导入http模块
var http=require('http');
//导入url模块
var url=require('url');
var log=require('./log4js_readconfig');
var querystring = require("querystring");
http.createServer(function(request,response){
request.setEncoding('utf-8');
var postData = "";
// 数据块接收中
request.on("data", function (postDataChunk) {
postData += postDataChunk;
});
request.on("end", function () {
console.log('数据接收完毕');
var params = querystring.parse(postData);//GET & POST 解释表单数据部分{name="zzl",email="zzl@sina.com"}
console.log(params);
console.log(params["name"]+"~~"+params["password"]);
response.writeHead(200,{'Content-Type':'text/html;charset=utf-8'});//设置response编码为utf-8
response.write("======================================="+params["name"]+"~~"+params["password"]);
response.write(util.inspect(params));
response.end("数据提交完毕");
});
}).listen(3000);
console.log("--HTTP NodeJS Connect--");
发送get请求
/**
*发送一个get请求
* 127.0.0.1:3000/http_get?name=小小沉沉&password=qwer
*/
//导入http模块
var http=require('http');
//导入url模块
var url=require('url');
var log=require('./log4js_readconfig');
log.info.info("~~log4js load success~~");
http.get({
hostname: 'localhost',
port: 8089,
path: '/news/newslist/PY_SelectNews?pgnum=1&cnt=15',
agent: false // 创建一个新的代理,只用于本次请求
}, function(response) {
// 对响应进行处理
response.on('data', (resData) => {
//log.info.info(resData.toString());
var parsedData_1 = JSON.parse(resData.toString());
log.info.info(parsedData_1);
});
response.on('end', () => {
log.info.info('响应中已无数据。');
});
});
//实例2
http.get('http://localhost:8089/news/newslist/PY_SelectNews?pgnum=1&cnt=15', (res) => {
res.setEncoding('utf8');
var rawData = '';
res.on('data', (chunk) => { rawData += chunk; });
res.on('end', () => {
try {
var parsedData = JSON.parse(rawData.toString());
console.log(parsedData);
} catch (e) {
console.error(e.message);
}
});
}).on('error', (e) => {
console.error(`错误: ${e.message}`);
});
发送post请求
var http=require('http');
var querystring=require('querystring');
var postData="{'userinfo_id' : '1','contact_information' : '123','advise_text' : '王'}";
var options = {
hostname: 'localhost',
port: 8089,
path: '/news/user/PY_UserAdvise',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData)
}
};
var req = http.request(options, (res) => {
console.log(`状态码: ${res.statusCode}`);
console.log(`响应头: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`响应主体: ${chunk}`);
});
res.on('end', () => {
console.log('响应中已无数据。');
});
});
req.on('error', (e) => {
console.error(`请求遇到问题: ${e.message}`);
});
// 写入数据到请求主体
req.write(postData);
req.end();