The full name of the HTTP protocol is hypertext Transfer Protocol, Hypertext Transfer Protocol, which is a protocol used frequently by mobile clients and PC-side access to network resources. The HTTP protocol is one of the most commonly used protocols in network development, and its format is:/HTTP/HOST/resource path. The HTTP protocol specifies the format of data transfer between the client and the server, enabling the client and server to transmit data efficiently. Examples of communication between the client and the server are:
The HTTP protocol has the following features:
1, simple and fast, because the HTTP protocol is simple, so the HTTP server program size is small, communication speed is relatively fast.
2, flexible, HTTP allows the transmission of any type of data.
3, HTTP0.9 and 1.0 use a non-persistent connection, limit each connection to only one request, the server to the client's request after the corresponding, immediately disconnect, this way can save transmission time. (The HTTP protocol is a stateless protocol.) )
The following is the process of HTTP communication, in order to use the HTTP protocol must first understand the Protocol's complete communication process. The complete HTTP communication process can be divided into 2 major steps:
1. Request
The client sends a message to the server, requesting the server to handle it accordingly.
2. Response
The server responds to the messages received by the client and handles them accordingly. (It is generally necessary to return some data content.) )
Next, the two steps are respectively.
1. HTTP communication Process--Request
The HTTP protocol stipulates that a complete HTTP request sent to the server by a client includes the following:
A, request line
B, Request header
c, the request body
Here is a look at a request to get Baidu Logo:
The request line contains the request method, the request resource path, and the HTTP protocol version. In the above request, the first line is the request line:
Get/img/bd_logo1.png http/1.1
The request method is the Get method, and the request resource path is the/img/bd_logo1.png,http protocol version is http/1.1.
The request header is the section below the request line above, containing information such as the host address of the client request, the environment description for the client, and so on.
Host:www.baidu.com---------The server host that the client wants to access
User-agent: ...---------client-type/client-side software environment
Accept:image/png.----------the type of data the client can receive
Accept-language:zh-cn,zh.-------a client's locale
Accept-encoding:gizp,deflate data compression format supported by-------Client
referer:https://www.baidu.com/-------from which page, browser commonly used for statistics
Cookies: ...------because HTTP is stateless, use cookies to identify users
connection:keep-alive------remain connected, HTTP version 1.1. Because of the non-persistent connection used by HTTP, although it saves time, it is inefficient and introduces this field in order to solve this problem.
Request the individual client to send the specific data to the server, usually can be seen when the request method is post. The following HTTP request is made with a login:
The parameter content displayed by post is the request body.
2. HTTP communication Process--response
The client sends a request to the server and the server makes the appropriate operation, typically returning the data to the client. The HTTP protocol specifies that a complete HTTP response should contain the following:
A, status line (response line)
B, Response header
C, Response Body (entity content)
below to see a response to visit Baidu homepage:
The status line includes the version, status code, and Status description of the HTTP protocol, such as the status line for this response:
http/1.1 0K
http/1.1 is the version of the Protocol, 200 is the status code, OK is the status description. (The meaning of the status code represents the back given.) )
The response header is the status line in the above response, including the description of the server, the description of the return data, the following a few more important, the other can be Baidu or access to the corresponding documents.
server:bfe/1.0.8.5--------Server Environment
Date:mon, June 05:34:28 GMT-----Response Time GMT is Greenwich, Beijing is the East eight district, the conversion needs gmt+8 hours.
content-type:image/png------The data type returned
content-length:7877------The length of the returned data
The response body is the server returned to the client's specific data, the response of this request is Baidu logo.
The following diagram can be used to facilitate the understanding of the HTTP communication process:
The following are common response status codes:
1XX: Indication information--Indicates that the request has been received and continues processing
2XX: Success-Indicates that the request has been successfully received, understood, accepted
3XX: Redirect--further action is required to complete the request
4XX: Client Error--Request syntax error or request not implemented
5XX: Server-side error-the server failed to implement a legitimate request
Common status codes, status descriptions, descriptions:
$ OK//client request succeeded
Bad Request//client requests have syntax errors and cannot be understood by the server
401 Unauthorized//request unauthorized, this status code must be used with the Www-authenticate header field
403 Forbidden//server receives request, but refuses to provide service
404 Not Found//request resource not present, eg: Wrong URL entered
Internal Server error//server unexpected errors
503 Server Unavailable//server is currently unable to process client requests and may return to normal after some time
eg:http/1.1 OK (CRLF)
HTTP protocol (i)