Some basic concepts of http

http

It is a hypertext transfer protocol, which can also be called a request-response protocol. The page we see is the client side. The user sends information to the server side through http requests, and then gets the information from the server side and responds on the client side.

The Prime Minister looks at a simple url: http://www.baidu.com/index/helloword;

Let's analyze it step by step:

1. Look at the first part of the url: http, which is a mode of the url, indicating that the http request mode is being used.

2. Looking at www.baidu.com, it actually means the IP address of the server you are querying and the port number used. DNS database to convert. When the user sends a request, it will query the server to send the request through DNS, and get the IP address;

3. The last part is the local path of the resource you are requesting.

 

http has its own status code, the following are four common status codes:

1.200 OK means a successful response to the information you requested.

2.302 redirect.

3.404 NOT FOUND The page was not found

4.500 This is a server-side error.

 

http nodejs get post request

 

var http = require('http');  
 
var options = {  
    hostname: '192.168.1.31',//The IP address of the service you are requesting  
    port: 3000, //The port number of the service you are requesting
    path: '/index/helloword/, //path
    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()};//This is the data that needs to be submitted  
  
  
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();  

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327012867&siteId=291194637