A preliminary study of http protocol

1. HTTP, the Hypertext Transfer Protocol, is a stateless, application-layer protocol based on the request and response mode. Most web development is a web application built on top of the HTTP protocol.

2. The working process of HTTP: When we request a hyperlink, HTTP starts to work. The client first sends a request to the server. The request content includes: protocol version number, request address, request method, request header and request parameters; After receiving the request, the server performs corresponding processing and returns the response data to the client. The response content includes: protocol version number, status code and response data. The front end performs corresponding processing according to the response data, which is what we finally see. These processes are done automatically by HTTP, we just enter or click on the request address and see what the front end shows us

3. The main features of the HTTP protocol can be summarized as follows:

  • Client/server mode is supported.
  • Simple and fast: When a client requests a service from the server, it only needs to transmit the request method and path. Commonly used request methods are GET, HEAD, POST, DELETE . Each method specifies a different type of contact between the client and the server. Because the HTTP protocol is simple, the program scale of the HTTP server is small, so the communication speed is fast.
  • HTTP allows the transmission of data objects of any type. The type being transferred is marked by Content-Type.
  • Connectionless: The meaning of connectionless is to limit the processing of only one request per connection. After the server processes the client's request and receives the client's response, it disconnects. In this way, transmission time can be saved.
  • Stateless: The HTTP protocol is a stateless protocol. Stateless means that the protocol has no memory capability for transaction processing. The lack of state means that if previous information is required for subsequent processing, it must be retransmitted, potentially resulting in an increased amount of data transferred per connection. On the other hand, the server responds faster when it does not need the previous information.

4. HTTP status codes are mainly divided into 5 categories: those starting with 1 indicate that the request has been accepted and need to continue processing; those that begin with 2 indicate that the request has been successfully received, understood, and accepted by the server; those that begin with 3 indicate that the client is required Take further action to complete the request; those starting with 4 indicate that the client may seem to have an error, preventing the server from processing; those starting with 5 indicate that the server has an error or abnormal state in the process of processing the request, and some It may be that the server realizes that the request cannot be processed with the current hardware and software resources.

  • Common ones are: 200: The server successfully processed the request; 404: The resource was not found; 500: Internal server error; 503: The server is currently unable to serve the request; 302: The requested URL has been temporarily transferred; 304: The client's cached resource is up-to-date and requires the client to use the cache
5. An HTTP URL (a URL is a special type of URI that contains enough information to find a resource) has the following format:
http://host[":"port][abs_path ] 

http means to locate network resources through the HTTP protocol; host means a legitimate Internet host domain name or IP address; port specifies a port number, if it is empty, the default port 80 is used; abs_path specifies the URI of the requested resource; if not given in the URL abs_path, then when it is used as a request URI, it must be given in the form of "/", usually this work browser does it automatically for us.

6. The http request consists of three parts: the request line, the message header, and the request body

  • A request line begins with a method symbol, separated by spaces, followed by the requested URI and the version of the protocol, in the following format: Method Request-URI HTTP-Version CRLF . Where Method represents the request method; Request-URI is a Uniform Resource Identifier; HTTP-Version represents the requested HTTP protocol version; CRLF represents carriage return and line feed (except for the trailing CRLF, separate CR or LF characters are not allowed) .
  • 请求方法(所有方法全为大写)有多种,各个方法的解释如下:
  • GET     请求获取Request-URI所标识的资源
  • POST    在Request-URI所标识的资源后附加新的数据
  • HEAD    请求获取由Request-URI所标识的资源的响应消息报头
  • PUT     请求服务器存储一个资源,并用Request-URI作为其标识
  • DELETE  请求服务器删除Request-URI所标识的资源
  • TRACE   请求服务器回送收到的请求信息,主要用于测试或诊断
  • CONNECT 保留将来使用
  • OPTIONS 请求查询服务器的性能,或者查询与资源相关的选项和需

7.HTTP响应也是由三个部分组成,分别是:状态行、消息报头、响应正文

  • 状态行格式如下:HTTP-Version Status-Code Reason-Phrase CRLF. 其中,HTTP-Version表示服务器HTTP协议的版本;Status-Code表示服务器发回的响应状态代码;Reason-Phrase表示状态代码的文本描述。
  • 状态代码有三位数字组成,第一个数字定义了响应的类别,且有五种可能取值:
  • 1xx:指示信息--表示请求已接收,继续处理
  • 2xx:成功--表示请求已被成功接收、理解、接受
  • 3xx:重定向--要完成请求必须进行更进一步的操作
  • 4xx:客户端错误--请求有语法错误或请求无法实现
  • 5xx:服务器端错误--服务器未能实现合法的请求常见状态代码、状态描述、说明
  • 200 OK      //客户端请求成功
  • 400 Bad Request  //客户端请求有语法错误,不能被服务器所理解
  • 401 Unauthorized //请求未经授权,这个状态代码必须和WWW-Authenticate报头域一起使用
  •  403 Forbidden  //服务器收到请求,但是拒绝提供服务
  • 404 Not Found  //请求资源不存在,eg:输入了错误的URL
  • 500 Internal Server Error //服务器发生不可预期的错误
  • 503 Server Unavailable  //服务器当前不能处理客户端的请求,一段时间后可能恢复正常

 





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324669933&siteId=291194637