/**
- nodejs模拟表单提交 */ var http = require('http'); var querystring = require('querystring'); var contents = querystring.stringify({ p:'2', na: '看阿奎', ad: '成都市锦江区天府广场人民中路5段67耗' }); //console.log(contents); var options = { host: '127.0.0.1', port: '80', //path: ', path: '', method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length' : contents.length } }; var req = http.request(options, function(response) { response.setEncoding('utf8'); console.log("Got response: " + response.statusCode); response.on('data', function (data) { console.log(data); }).on('end', function(){ console.log(response.headers); }); }) req.write(contents); req.end();
/* http.get(options, callback) http 模块还提供了一个更加简便的方法用于处 理GET请求:http.get。它是 http.request 的简化版,唯一的区别在于http.get 自动将请求方法设为了 GET 请求,同时不需要手动调用 req.end()。 //httpget.js var http = require('http'); http.get({host: 'www.byvoid.com'}, function(res) { res.setEncoding('utf8'); res.on('data', function (data) { console.log(data); }); });
*/