HTTP defines

Name: Hypertext Transfer Protocol

Chinese name: Hypertext Transfer protocol

hypertext

Hypertext, or “extended text,” refers to HTML that can have links to other text

agreement

Protocol: a standard established by two communicators to enable smooth communication between two communicators. Finally, HTTP is an object-oriented protocol at the application layer.

How HTTP works

The browser

After the user enters the address, press Enter or click the link. -> The browser assemgates HTTP packets and sends a request to the server. -> The server processes the request and sends a response packet to the browser

This server, when YOU press Enter, DNS will resolve the domain name and get the IP address of the server.

The phone App

The user clicks or the interface automatically triggers networking requirements -> Android code calls to assemble HTTP packets and send them to the server -> The server processes the request and sends response packets to the mobile phone -> Android code processes the corresponding packets and makes corresponding processing (such as storing data, processing data and displaying data to the interface)

URL and HTTP packets

URL format

Three parts: Protocol type, server ADDRESS (and port number), path Protocol type :// Server address [: port number] path

http://github.com/mariolu0605/ShapeLoading: HTTP server address: github.com path: mariolu0605 / ShapeLoadingCopy the code

The HTTP message

Message format

The request message

The response message

Request Method

GET
  • Used to obtain resources
  • The server data is not modified
  • Don’t send the body
  • Power etc.

POST
  • Used to add or modify resources
  • The content sent to the server is written in the body
  • The power etc.

PUT
  • Used to modify resources
  • The content sent to the server is written in the body
  • Power etc.

So the question is, what’s the difference between POST and PUT?

PUT is idempotent,POST is non-idempotent. Take 🌰 : for example, when accessing an API, we need to send a POST or PUT method to a URL. We need to decide whether we need idempotent or not based on our needs. For example, if we send two requests to github. Mariolu/Users, if two results are produced on the server, then it is not idempotent, and if the second request overrides the first, then it is idempotent. In the former case, the POST method should be used; in the latter case, the PUT method should be used. We need to code according to rules, so REST is referenced later.

DELETE
  • Used to delete a resource
  • Don’t send the body
  • Power etc.

HEAD
  • The usage is exactly the same as using GET

Difference: Using HEAD saves resources when we only need to retrieve information that is not body in the response returned. For example, 🌰 : when we visit the server, we can use HEAD access first to check whether the local cache and the server content are consistent, if so, directly load the local content.

Status Code Indicates the Status Code

A three-digit number for a typed description of the response result

  • 1xx: Temporary message. Such as 100 (continue to send) 101 (switching protocol)
  • 2 xx: success. For example, 200 (request succeeded) and 201 ().
  • 3xx: redirection. For example, 301 (permanently moved), 302 (temporarily moved), 304 (unchanged).
  • 4xx: Client error. For example, 400 (client request error), 401 (authentication failure), 403 (forbidden), 404 (resource cannot be found).
  • 5xx: server error, such as 500 (internal server error).

The Header first

Function: Metadata of HTTP messages.

Metadata Raw data. Describes attributes of data.

HOST

Target host. Note: not for addressing on the network, but for locating sub-servers on the target server.

Because one IP address can correspond to many sub-servers (multiple domain names). For example, 🌰 : 192.168.31.111 corresponds to www.mariolu01.com and www.mariolu02.com, which access the same IP address, but need to locate the child server to be accessed. In this case, HOST is needed to locate the child server.

Content-Type

Specify the type of body. There are four main categories

  • text/html

Return the type of response when requesting a Web page, with HTML text returned in Body. The format is as follows:

  • x-www-form-urlencoded

Web page pure form submission, such as page login

<form method="POST" enctype="application/x-www->form-urlencoded">.</form>
Copy the code

The format is as follows:

  • multipart/form-data

Submission method with binary files, such as profile picture upload, file upload, etc.

<form method="POST" enctype="multipart/form-data">.</form>
Copy the code

The format is as follows:

The boundary in the Content-Type is the separator. Why do you need a delimiter?

Because when you send a request, just knowing the total length, like the one above, and passing in a text and an image, there’s no way to tell the two apart without a separator.

  • application/json , image/jpeg , application/zip …

Submit JSON in the request

Json is returned in response

Submit binary content in the request

Content-Length

Specify the length of the Body in bytes. So why do we need content-Length? Why not just end with an identifier? Since the Body may contain some binary data, using some identifier (such as \n,*, etc.) to indicate the end may have the same representation as some binary data, resulting in the end of reading the Body before the end.

Transfer:chunked(block Transfer code)

When the response is initiated, the length of the content has not yet been determined. The purpose is to give the response as early as possible to reduce the user’s wait for images.jpg

End with a zero

Location

Specifies the destination URL for the redirect

User-Agent

The user agent is the person who actually sends the request and receives the response, such as a mobile browser or a mobile App. This Header can be used to determine the actual sender and determine the page size. For example, the same website may display different pages on the PC and mobile phone.

Range/Accept-Range

Accept-range: bytes Indicates that the server supports accept-range data in bytes. Bytes =



Content-range :



/total Content-range :



/ Total Content-range :



/ Total Content-range :< end>







Other Headers

  • Accept: Indicates the data type accepted by the client. Such as text/HTML
  • Accept-charset: character set accepted by the client. Such as utf-8
  • Accept-encoding: Indicates the Encoding type accepted by the client. Such as gzip
  • Content-encoding: Indicates the compression type. Such as gzip