OKHttp series of analysis we are going to start from six places, we will start with the basics of the network, because OKHttp involves Http protocol, so for the basic knowledge of Http, do a simple understanding, is very necessary. I recommend three books: Illustrated Http, The Definitive GUIDE to Http, and Java Web Programming Basics.

####1.OSI’s seven-layer model


2.TCP three-way handshake and four-way wave


3. Overview of Http packets


HTTP transmits data in the form of HTTP packets, which are called request packets. The packets on the server are called response packets. Since it is a message, it must have a format:

Request: Request header + blank line + Request content Response: Response header + blank line + Response content

Request content and response content this should not be too much introduction, development should know, then the request header is exactly what we know, but when it comes to the specific parameters, not all know its function, so let’s look at the request header and response header in detail. Here we need to build a background to do testing, why to write their own interface? First, we can learn how to write the background interface. Second, cookies, Session and Token must be combined with the background to test the effect. It is impossible to memorize, we still need to verify by ourselves. The specific operation is in the video link at the end. So what’s in the HTTP header?

# # # # 4. Http header


HTTP header field contains very rich information (more), his mother, we can open a browser to observe ourselves, so you will have a more intuitive feel, anyway, the first field is very important, no matter you are to write a network framework, or analyze OkHttp source need to know in these fields, Here I list some of the more common ones to explain:

Accept-charset: preferred character set accept-language: preferred natural Language Accept-encoding: If-modified-since: compares the update time of the resource if-range: sends the Byte Range of the entity when the resource is not updated Request Cookie: sets the Cookie (covered separately below)

3.2 Cache-control: controls the behavior of the Cache (discussed separately below) set-cookie: Set Cookie Location: redirects a client to a specified URI Expires date and time the entity body Expires Last-Modified Date and time the resource was Last Modified Status Code: the Status Code of the response

####4. Cookies, sessions, and tokens


4.1 Cookies, sessions, and tokens are all used for persistent processing, so that clients and servers can get to know each other. Http requests are by default non-persistent and stateless, and no one knows who. 4.2 Cookie: it is the information stored on the client. The server sends it to the client through the set-cookie field in the response header. If the Cookie has expired, it will generally be clear. 4.3 Session: temporary client information stored on the server, the user will be cleared after leaving the website. 4.4 Token (App) “Token” : authentication of user identity, similar to Cookie, relatively more secure, general process: 3.4.1 The Client Applies for a Token from the Server 3.4.2 Upon receiving the request, the server verifies the user information and issues a Token to the client. The server also saves the Token. 3.4.3 The client saves the Token after receiving the request. 3.4.4 When receiving other requests, the server will verify the client’s Token. If the request is successful, the server will return data, but if the request is unsuccessful, it will do nothing or perform other processing

# # # # 5. Http cache


About the cache to see this article is: mp.weixin.qq.com/s/qOMO0LIdA… There are two important fields related to caching: cache-control: Public, private, no-cache, max-age, no-store Expires: Specify the absolute time when the cached data is valid, and tell the client that the local cache is invalid after this point (compared to the client time). At this point, the client can consider the cached data valid and load it directly from the cache.

There is another special case, if there is a local cache, but the cache expires, the server will send a request to the server again, but if the server data has not changed, it will not necessarily give you the data, but will return the status code 304, so in what case will 304 be returned? This is “on the importance of writing their own interface” we write a test interface, test can see this result.

####6. Http status code


1xx: Infomational, the received request is being processed. 2xx: Succeed, the request is successfully processed. For example, 200,204 3xx: 304 (Not,modified) 307 4xx: Client Error, the server cannot process the request, such as 404,405, 5xx: Server Error (Server side Error), Server processing request Error, such as 500,502

####7. The difference between Http and Https


Https = Http + encryption + Authentication + Integrity

Disadvantages of Http: 7.1 Data is not encrypted transmission, may encounter eavesdropping; 7.2 If the identity of the communication party is not verified, it may encounter camouflage; 7.3 Packet Integrity Cannot be Verified and May be tampered with.

####8. Http 1.x and Http 2.0 differences


All shared outline: Android Advanced Journey – System Architecture chapter

Video on address: http://pan.baidu.com/s/1dF8NeRR