In this paper, the theoretical knowledge of Transport layer TCP protocol is introduced, and the theoretical knowledge of HTTP protocol and HTTPS protocol based on TCP protocol is introduced.
The HTTP protocol defines the way the client requests data from the server based on the TCP protocol, which is a transaction-oriented application layer protocol, which is flexible, simple, fast, non-connected and stateless, and is the basis for exchanging all kinds of data in the network.
Request and Response messages for HTTP protocol
The format of the HTTP message is as follows:
We can see that the message of the HTTP protocol is mainly divided into two parts: the message header and the message body, and the middle is separated by a blank line. Let's take a look at the request message and the response message in detail:
Request message
The request message consists of three parts: the request line, the request header, the message body (request data). As shown
The request line includes the request method, URI, protocol version Three parts of the information.
The request method includes the following eight types, commonly used are get and post:
- Get request gets the resource identified by the Request-uri
- Post appends new data to the resource identified by Request-uri
- HEAD request Gets the response message header for the resource identified by Request-uri
- PUT Request server stores a resource and uses Request-uri as its identity
- Delete Request server deletes the resource identified by the Request-uri
- TRACE requests the server to echo received request information, primarily for testing or diagnostics
- CONNECT reserved for future use
- Options request the performance of the query server, or query for resource-related choices and requirements
The requested URI identifies the location of the requested resource, and the protocol version primarily has http/1.0 and http/1.1 two versions.
The request Header contains various information about the request, such as
User-agent: The type of browser that generated the request.
Accept: A list of content types that the client can identify.
Host: The hostname of the request, which allows multiple domain names to be located in the same IP address as the virtual host.
The message body contains the requested data, mainly in the Post method, applicable to the submission of forms and so on.
Response message
The response message is also composed of three parts: the status line, the response header, and the message body as shown in:
The status line includes the HTTP protocol version, the response status code, and the text description of the status code. There are five types of 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
The response header contains various information about the response. The message body contains the response data that needs to be transmitted, such as HTML, binary data, and so on.
After introducing the HTTP protocol request and response messages, let's look at the interaction between the HTTP client and the server as an example of the browser request HTML page:
- client browser parses URLs, resolves addresses to DNS requests
- Domain Name System DNS resolves the IP address of the server back to the client
- The client establishes a TCP connection with the server and sends a GET request message
- The server responds and sends the corresponding HTML file to the client browser
- Releasing a TCP connection
- Browser Display HTML file
HTTP uses the TCP protocol to ensure reliable transmission, but it is not connected in itself, which means that the two parties do not need to establish a connection before sending the message. At the same time, the HTTP protocol is stateless, the server does not save the client information, it is easier to support a large number of concurrent HTTP requests. In HTTP1.0, each time a connection object is requested, a connection is initiated, which causes the request to be inefficient. In HTTP1.1, a persistent connection is used, which means that the server remains connected for a period of time after responding to a client request, so that the client and the server can maintain a connection over time. HTTP1.1 protocol Continuous Connection There are two ways to work:
- Non-pipelined: The client sends the next request after receiving a response message. After the server has sent an object, the TCP connection is idle and the server resource is wasted.
- Pipeline method: The client can send the request message continuously before receiving the response message, which improves the transmission efficiency.
Cookies
The above mentioned HTTP protocol is stateless, regardless of the client or the server does not log HTTP-related information. However, cookies can be used to save information on the client and server side, the mechanism is as follows:
A cookie is a small text file that is used to store content determined by the server at the client, and the request message automatically adds the contents of the cookie.
HTTPS protocol
The HTTP protocol uses clear text during transmission and does not have authentication and integrity validation. And HTTPS is the use of the SSL Protocol HTTP protocol, although the sacrifice of speed, but to ensure the security of the data, applicable to the financial payment environment.
After summarizing the HTTP protocol, the next article will cover the httpclient and urlconnection libraries that will be used in Android development.
Android Network Programming Caprice (2)