nodejs http.request 发送请求demo
2016-06-06 15:42
603 查看
接收参数:
option 数组对象,包含以下参数:
host: 表示请求网站的域名或IP地址(请求的地址)。 默认为'localhost'。
hostname: 服务器名称,主机名是首选的值。
port: 请求网站的端口,默认为 80。
localAddress: 建立网络连接的本地
socketPath: Unix Domain Socket(Domain套接字路径)
method: HTTP请求方法,默认是 ‘GET'。
path: 请求的相对于根的路径,默认是'/'。QueryString应该包含在其中。例如:/index.html?page=12
headers: 请求头对象。
auth: Basic认证(基本身份验证),这个值将被计算成请求头中的 Authorization 部分。
callback : 回调,传递一个参数,为 http.ClientResponse的实例。http.request 返回一个 http.ClientRequest 的实例。
GET请求
Js代码
![](http://qson.iteye.com/images/icon_star.png)
var http = require('http');
var qs = require('querystring');
var data = {
a: 123,
time: new Date().getTime()};//这是需要提交的数据
var content = qs.stringify(data);
var options = {
hostname: '127.0.0.1',
port: 10086,
path: '/pay/pay_callback?' + content,
method: 'GET'
};
var req = http.request(options, function (res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(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);
});
req.end();
POST请求
Js代码
![](http://qson.iteye.com/images/icon_star.png)
var http = require('http');
var qs = require('querystring');
var post_data = {
a: 123,
time: new Date().getTime()};//这是需要提交的数据
var content = qs.stringify(post_data);
var options = {
hostname: '127.0.0.1',
port: 10086,
path: '/pay/pay_callback',
method: 'POST',
headers: {
'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: ' + JSON.stringify(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();
option 数组对象,包含以下参数:
host: 表示请求网站的域名或IP地址(请求的地址)。 默认为'localhost'。
hostname: 服务器名称,主机名是首选的值。
port: 请求网站的端口,默认为 80。
localAddress: 建立网络连接的本地
socketPath: Unix Domain Socket(Domain套接字路径)
method: HTTP请求方法,默认是 ‘GET'。
path: 请求的相对于根的路径,默认是'/'。QueryString应该包含在其中。例如:/index.html?page=12
headers: 请求头对象。
auth: Basic认证(基本身份验证),这个值将被计算成请求头中的 Authorization 部分。
callback : 回调,传递一个参数,为 http.ClientResponse的实例。http.request 返回一个 http.ClientRequest 的实例。
GET请求
Js代码
![](http://qson.iteye.com/images/icon_star.png)
var http = require('http');
var qs = require('querystring');
var data = {
a: 123,
time: new Date().getTime()};//这是需要提交的数据
var content = qs.stringify(data);
var options = {
hostname: '127.0.0.1',
port: 10086,
path: '/pay/pay_callback?' + content,
method: 'GET'
};
var req = http.request(options, function (res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(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);
});
req.end();
POST请求
Js代码
![](http://qson.iteye.com/images/icon_star.png)
var http = require('http');
var qs = require('querystring');
var post_data = {
a: 123,
time: new Date().getTime()};//这是需要提交的数据
var content = qs.stringify(post_data);
var options = {
hostname: '127.0.0.1',
port: 10086,
path: '/pay/pay_callback',
method: 'POST',
headers: {
'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: ' + JSON.stringify(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();
相关文章推荐
- HttpClient 发送 HTTP、HTTPS 请求的简单封装
- 网络连接时套接字对的变化状态
- C# HttpClientHelper请求
- Fiddler学习_模拟网络限速+设置接口404+地址替换+修改返回数据
- HttpUtils addHander参数进行压缩数据,
- Android DefaultHttpClient 上传文件
- jboss5可以使用http://localhost/和http://127.0.0.1/ 不可以ip访问
- swift--用 Swift 编写面向协议的网络请求
- 网络带宽
- https http请求 demo
- Tcp为什么要三次握手四次挥手
- React Native第一个Demo(2)网络获取数据和ListView
- SVN使用MySQL验证用户名密码(HTTP)
- http 返回的状态值以及Tomcat webapp的管理
- 类似restful风格的网络请求框架
- LINUX 中的 TCP/IP协议 参数详解
- TCP洪水攻击(SYN Flood)的诊断和处理
- iOS模拟网络差
- Ghost windows7系统的连接网络的情况下网络连接图标为什么显示无网络访问
- HTTPS攻击