http的一些基本概念

http

它是一种超文本传输协议,也可以叫请求响应协议,我们所看到的页面是客户端,用户通过http请求将信息发送到服务端,再从服务端得到信息并在客户端响应。

首相看一个简单的url:http://www.baidu.com/index/helloword;

我们来一步一步分析:

1.看url的第一部分:http,它是url的一种模式,表示正在使用的是http请求模式。

2.在看www.baidu.com这一段,它其实包含的意思是你所查询的服务端的IP地址和所用的端口号,之所以会展现这种对于我们来说友好的方式,是通过一个叫DNS的数据库来转换的。在用户发送请求的时候,会通过DNS来查询想那干服务端发送请求,得到IP地址;

3.最后一部分就是你所请求资源的本地路径。

http有自己的状态码,下面是四种常见的状态码:

1.200 OK 代表你所请求的信息成功响应。

2.302 重定向。

3.404 NOT FOUND 未找到该页面

4.500 这就是服务端的错误。

http nodejs get post 请求

var http = require('http');  
 
var options = {  
    hostname: '192.168.1.31',//你所请求服务的IP地址  
    port: 3000,  //你所请求服务的端口号
    path: '/index/helloword/,  //路径
    method: 'GET'  
};  
  
var req = http.request(options, function (res) {    
    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();  
var http = require('http');  
   
var post_data = {  
    a: 123,  
    time: new Date().getTime()};//这是需要提交的数据  
  
  
var content = post_data;  
  
var options = {  
    hostname: '192.168.1.31',  
    port: 3000,  
    path: '/index/helloword',  
    method: 'POST',  
    headers: {  
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'  
    }  
};  
  
var req = http.request(options, function (res) {   
    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();  

猜你喜欢

转载自727798013.iteye.com/blog/2322858