Objective
HTTP network communication protocol plays a very important role in any development work, today, we will explain the relevant knowledge about HTTP.
Directory
5 minutes full understanding of HTTP knowledge. PNG computer network related knowledge
The computer network architecture is divided into five layers, with top-down applications, transports, networks, data links, and physical layers, such as:
Five-tier Protocol architecture
HTTP is present at the application layer at the top level, with a brief introduction to the application layer:
HTTP INTRODUCTION 1. Defined
That is hypertext Transfer Protocol, Hypertext Transfer Protocol, which belongs to the application layer protocol.
2. Role
Provides guidelines for applying interprocess communication (Request & Response)
3. Features
- No connection: HTTP itself is not connected, that is, there is no need to establish an HTTP connection before exchanging HTTP messages
- Stateless: The HTTP protocol is stateless: During the data transfer process, no historical information and status information is saved. Stateless features simplify the design of servers, making it easier for servers to support a large number of concurrent HTTP requests.
- High Transmission reliability: TCP is used as Transport layer protocol (connection-oriented, reliable transmission), which requires a pre-established TCP connection when exchanging messages
- Good compatibility: Support B/s mode and C/s mode;
- Simple and fast: When a customer requests a service from the server, it simply transmits the request method and path. The request method commonly has, POST
- Flexible: HTTP allows the transfer of any type of data object
4. How to Work
The HTTP protocol takes the form of request/response work, workflow
Index.png5. HTTP message Explanation
HTTP messages are divided into request messages and response messages
5.1 Composition of HTTP request messages for HTTP request messages
HTTP request message composition. png
- Request line: Used to declare "request message", host domain name, resource path, and protocol version
- Request Header: A description of a client, server, or part of a message
- Request body: Used to store data information that needs to be sent to the server
1. Request Line
The composition of the request line
Spaces cannot be saved
1.1 Request method
That is, the request object operation, the request method has 8 kinds:
Method Category |
meaning |
OPTION |
Request information for "options" |
HEAD |
Request to read "header of URL flag information" information |
GET |
Request to read information about "URL flags" |
POST |
Adding information to a server |
PUT |
Adds (stores) a document under the specified URL |
DELETE |
Deletes the information flagged by the specified URL |
TRACE |
Request message for loopback testing |
CONNECT |
For proxy Server |
The most common is the Get and post methods .
1.2 Request Path
To understand the request address, first understand the following URL concepts:
- Definition: Uniform resoure Locator, Uniform Resource Locator, is a voluntary location of the abstract unique recognition method.
- Role: Methods used to represent resource locations and access to these resources
- Composition
< protocol >://< host >:< port >/< path >
- Protocol: The application-layer communication protocol used, such as the URL address under the HTTP protocol:
http://< host >:< ports >/< paths >
- Host: The domain name of the host on which the resource is requested
- Ports and paths can sometimes be omitted (HTTP default port number is 80)
As you can see from the above, the path is the part of the symbol "/" after the port number, for example
URL (Uniform Resource Locator) | Path
(Paths) |
http://www.baidu.com/ |
/ |
Http://www.weibo.com/2874748/home |
/2874748/home |
1.3 Protocol version
The HTTP protocol version is mostly 1.0, 1.1, 2.0
The specific difference here is not much to explain, we are interested to understand the next
Request Line Example
First assume:
- The URL address is: http://www.tsinghua.edu.cn/chn/yxsz/index.htm
- Request message with Get method
- Request message with HTTP1.1 version
The request line is: get/chn/yxsz/index.htm http/1.1
2. Request Header
- Role: A description of a client, server, or message part of the information
- How to use: Using header (field name): Value (value)
- Common Request Headers
1. Generic header for request and response messages
Generic header for request and response messages
2. Common Request Header
Common Request Header
- Example:
(URL address: http://www.tsinghua.edu.cn/chn/yxsz/index.htm)
Host:www.tsinghua.edu.cn (indicates host domain name)
user-agent:mozilla/5.0 (indicates that the user agent is using the Netscape browser)
3. The request body
- Function: Used to store data information that needs to be sent to the server
- How to use: At present, there are three kinds of
1. Data Interchange Format
The request body can be of any type, but the server needs additional parsing, such as JSON
{"skill":{ "web":[ { "name":"html", "year":"5" }, { "name":"ht", "year":"4" }], "database":[ { "name":"h", "year":"2" }]`}}
To learn more about JSON parsing in Android development you can see another article I wrote:
Android Development: JSON introduction and the most comprehensive analytic Method!
2. Key-value pair form
A "=" connection between keys and values, with & connections between each key-value pair, and only ASCII characters, such as query String
key1=value1&key2&value2
3. Partial form
The request body is divided into several parts, the application scenario is file upload, such as mail upload and so on
- Each paragraph begins with--{boundary}
- And then the description header for that segment.
- Empty line after description header
- End of each paragraph with--{boundary}--
As follows:
Instances of request messages in partial format
In combination with the above mentioned request line, request header and request body, now assume that
- The URL address is: http://www.tsinghua.edu.cn/chn/yxsz/index.htm
- Request message with Get method
- Request message with HTTP1.1 version
- The request message wants to indicate that the host domain and user agent are using the Netscape browser
- The request body adopts the key-value pair form
The request message is as follows:
Composition of HTTP response message for request message 5.2HTTP Response message
HTTP response messages consist of. png
As can be seen from the above, in addition to the first line (Request line vs state line), the other structure of the response message is very similar to the request message. Where the response body is used to store data information that needs to be returned to the client.
1. Status line
Status line composition
Where the space cannot be saved
1.1 Protocol version
The HTTP protocol version is mostly 1.0, 1.1, 2.0
The specific difference here is not much to explain, we are interested to understand the next
1.2 Status Code
The status code is divided into 5 major categories:
category |
meaning |
1xx |
Represents an informational notification, such as a request received or being processed |
2xx |
To express success, as accepted or known. |
3xx |
Indicates redirection, and further action must be taken to complete the request |
4xx |
Customer errors, such as errors in the request syntax or inability to complete: 404 |
5xx |
Indicates a server error, such as a server failure cannot complete the request |
1.3 Status information
A simple explanation of the status code
Detailed status code information can be seen in detail
Example of status line
- http/1.1 202 Accepted (accepted)
- http/1.1 301 Bad Request (permanent transfer)
- http/1.1 404 Not Found (cannot be found)
2. Response header
- Role: A description of a client, server, or message part of the information
- How to use: Using header (field name): Value (value)
- Common Request Headers
1. Generic header for request and response messages
Generic header for request and response messages
2. Common response Header
Common response Header3. Response body
- Function: Used to store data information that needs to be returned to the client
- Use: And the request body is consistent, also divided into: any type of data interchange format, key-value pair form and partial form, here does not make too much description.
The HTTP knowledge you need to know is here!