初学nodejs之:post | get 请求
2014-04-24 00:00
369 查看
摘要: 初学nodejs之:post | get 请求
/**
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: 'http://127.0.0.1/mysql/index.php?'+contents,
path: 'http://127.0.0.1/mysql/index.php',
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);
});
});
*/
/**
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: 'http://127.0.0.1/mysql/index.php?'+contents,
path: 'http://127.0.0.1/mysql/index.php',
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);
});
});
*/
相关文章推荐
- NodeJS收发GET和POST请求的示例代码
- nodejs使用http模块发送get与post请求的方法示例
- nodejs的request创建的get和post请求,带参数
- 【我的前端初学填坑】2.get和post请求的区别
- nodejs之get/post请求的几种方式小结
- nodejs之express(二)get和post请求
- 使用nodejs发起get或post请求抓取网页内容,支持中文编码
- nodejs发起HTTPS,GET,POST请求Face++人脸识别
- nodejs之get/post请求的几种方式
- 初学 spring mvc 2(注解+get,post请求+重定向)
- #nodejs post或get 请求获取参数
- nodejs 使用http进行post或get请求(携带cookie)
- nodejs之get/post请求的几种方式
- nodejs之koa2请求示例(GET,POST)
- nodejs路由--get和post请求
- nodejs 发起http get post 请求
- HttpUrlConnection两种请求数据的方式get和post提交数据
- [Java] 模拟HTTP的Get和Post请求
- Http 之Get/Post请求区别
- jquery ajax GET POST 跨域请求实现