preface

  • HTTPNetwork communication protocols are very important in any development work
  • Today, I will offer oneHTTPI hope you’ll enjoy it
  • Ps: Recently, WE are preparing an activity called “Write Android with me”. We need the help of readers. I hope you can read the article and participate in it.I’d like to invite you to write Android with me

directory


1. Stock up on knowledge

Before explaining the HTTP protocol, learn some basic computer networking knowledge

1.1 Computer network architecture

  • Defines the layers of a computer network + its set of protocols

  • Function Defines the functions that the computer network can perform

  • The computer network architecture is divided into three kinds: OSI architecture, TCP/IP architecture and five layer architecture

  • OSIArchitecture: conceptually clear & ideologically complete, but complex & impractical
  • TCP / IPArchitecture: contains a set of network protocols that form the basis of the InternetInternetThe core protocol is widely used in LAN and WAN
  • Five-tier architecture: FusedOSITCP / IPArchitecture for learning & explaining computer principles

  • TCP / IPThe architecture is detailed as a resultTCP / IPThe architecture is more extensive, so the main explanation

1.2 Basic model of HTTP protocol communication

  • HTTP protocol transmission information based on the TCP/IP protocol model

  • HTTP belongs to the highest application layer


2. Introduction

Next, take a brief look at HTTP


3. Working mode

  • HTTPDeal withRequest/ResponseThe way in which
  • The specific working process is as follows:


4. HTTP packet details

  • HTTPData exchange at the application layer = packets
  • HTTPAre classified into request packets and response packets

When sending a request and when responding to a request

  • The following describes the two types of packets in detail

4.1 Request Message

4.1.1 Message Structure

  • HTTPThe request message is sent byRequest line, request header & request bodyComposition, as shown below

  • Each component is described in detail below

4.1.2 Structure Details

Component 1: Request line
  • Function declaration request method, host name, resource path & protocol version

  • Structure request line composition = request method + request path + protocol version

Note: Spaces cannot be omitted

  • Composition is introduced

The difference between GET and PSOT methods is specified here:

  • Example Setting: Request packets are adoptedGETMethods,URLAddress =www.tsinghua.edu.cn/chn/yxsz/in…HTTP1.1version

GET/CHN /yxsz/index.htm HTTP/1.1

Component 2: Request header
  • Function: Declare some information about the client, server, or packet
  • Usage: Use ** “header (field name) : value (value)”
  • Common request Headers 1. Common Headers of request and response packets

2. Common request Headers

  • Example: (URL: www.tsinghua.edu.cn/chn/yxsz/in… Host: www.tsinghua.edu.cn (indicates the Host domain name) User-agent: Mozilla/5.0 (indicates that the User Agent uses the Netscape browser)
Composition 3: Request body
  • Function: Stores data to be sent to the server

Optional parts, such as GET requests, have no request data

  • Usage: 3 kinds in total

At this point, the explanation about the request line, request header and request body of the request message is finished.

4.1.3 summary

  • The following table summarizes the request message

  • Sample Request message

4.2 HTTP Response Packets

4.2.1 Packet Structure

  • HTTPThe response message contains status line, response header and response body

  • The response header and body are similar to the request header and body of the request packet
  • The biggest difference between the two types of messages is the status line and the request line

Each component is described in detail below

4.2.2 Structure Details

Component 1: status line

  • Description Protocol version, status code, and status code description
  • The status line consists of protocol version, status code and status information

Where, Spaces cannot be saved

  • Detailed introduction

  • Example status line HTTP/1.1 202 Accepted, HTTP/1.1 404 Not Found

Component 2: Response header

  • Function: Declare some information about the client, server, or packet
  • Usage: Use ** “header (field name) : value (value)”
  • Common request Headers 1. Common Headers of request and response packets

2. Common response headers

Component 3: response body

  • Function: Stores data to be returned to the client
  • Usage: consistent with the request body, also divided into: any type of data exchange format, key-value pair form and partial form

4.2.3 Summary of Response Packets

4.3 summarize

The following two message structures are briefly summarized


5. Extra knowledge

Here are some additional facts about HTTP:

  • HTTP1.1HTTP1.0The difference between
  • HTTPHTTPSThe difference between
  • HTTPThe way to handle long connections

5.1 Differences between HTTP1.1 and HTTP1.0

Http1.1 has the following advantages over Http1.0:

  • Introduce persistent connections, that is, in the sameTCPCan transmit more than oneHTTPRequest & Response
  • Multiple requests & responses can occur simultaneously and overlap
  • Introduce more request & response headers

For example, HTTP1.0 does not have the host field related to authentication, state management, and Cache

5.2 Differences between HTTP and HTTPS

5.3 How to Process Long Connections using HTTP


6. Summary

  • This article provides a comprehensive overview of the basics of HTTP.

  • Next I will introduce the relevant knowledge of continuing Android development. If you are interested, please continue to pay attention to Carson_Ho’s Android development notes


Welcome to follow Carson_ho on wechat