Note: This article is a summary of the basics of HTTP. After all, IT is important to do the network

The main content of this article

  • What is the HTTP
  • The URL,
  • HTTP request text
  • HTTP response section

What is HTTP

1, an overview of the

HTTP stands for HyperText Transfer Protocal, namely: Hypertext Transfer Protocol, which has been widely used on the WWW since 1990, is the most widely used protocol on the WWW today. HTTP is an application layer protocol, and when you surf the Web, the browser and the Web server send and receive data over the Internet over HTTP. HTTP is a stateless protocol based on a request/response pattern. That’s what we call Request/Response

2, the characteristics of

  • Client/server mode is supported
  • Simple and fast: when a client requests services from the server, it only needs to send the request method and path. Because HTTP protocol is simple, the HTTP server program size is small, so the communication speed is very fast
  • Flexibility: HTTP allows the transfer of any type of data object. The Type being transferred is marked by content-Type
  • Connectionless: Connectionless means to limit processing to one request per link. After the server processes the request and receives the reply from the customer, the server disconnects from the customer. In this way, the transmission time is saved
  • Stateless: HTTP is a stateless protocol. Stateless means that the protocol has no memory for processing things. The lack of state means that if the previous information is needed for subsequent processing, it must be retransmitted, which can result in an increase in the amount of data transferred per connection. On the other hand, the server responds faster when it doesn’t need the previous information

Second, URL details

1, the introduction of

Uniform Resource Locator (URL) is a Uniform Resource Locator (URL). It is also called a Web address. It is like a door plate on the Network and a standard Resource address on the Internet

2. Basic composition

General format: scheme://host[:port#]/path/… /[?query-string][#anchor]

The name of the function
scheme Which protocol to use when accessing the server to obtain resources, such as HTTP, HTTPS, FTP, etc
host IP address or domain name of the HTTP server
port# The default port number of the HTTP server is 80. In this case, the port number can be omitted. If another port is used, specify itwww.cnblogs.com:8080
path Path to access resources
query-string Data sent to the HTTP server
anchor anchor

For example: www.mywebsite.com/sj/test/tes… Among them

The name of the Corresponding field
Schema http
host www.mywebsite.com
path /js/test/test.aspx
Query-string name=sviergn&x=true
anchor stuff

Let me draw a little bit more intuitive picture

An URL

HTTP request

An HTTP request packet is divided into three parts: the request line, the request header, and the request body

The request message

1. Request line

The Request line is divided into three parts: Request method, Request address, and protocol version

Request method

The HTTP/1.1 protocol defines eight methods (also known as “actions”) to manipulate a given resource in different ways

The method name function
GET The GET method should only be used to read data when making “display” requests to specified resources, and should not be used for “side effects” operations
POST Specify the resource to submit data and request the server to process it (for example, submit a form or upload a file). The data is contained in the request text. This request may create a new resource or modify an existing resource, or both.
PUT Uploads its latest content to the specified resource location
DELETE Requests the server to remove the resource identified by request-URI
OPTIONS Causes the server to return all HTTP request methods supported by the resource. with*Instead of the resource name, send an OPTIONS request to the Web server to test whether the server functions properly
HEAD As with the GET method, a request is made to the server for a specified resource, except that the server will not return the text portion of the resource. The advantage of this method is that it can be retrieved without having to transmit the entire contentInformation about the resource(Source information or metadata)
TRACE Displays requests received by the server, mainly for testing or diagnostics
CONNECT Reserved in HTTP/1.1 for proxy servers that can change connections to channel mode. Typically used for links to SSL encrypted servers (via an unencrypted HTTP proxy server)

PUT, DELETE, GET, and POST are the most common methods used by RESful interfaces

2. Request headers

The request header can be used to pass some additional information in the format: key: value, note that there is a space after the colon:

Request header

Common headers for requests and responses

The name of the role
Content-Type Type of the request body or response body, for example, Text /plain or Application/JSON
Accept Description received type, can be multiple values, with.(English comma) separate
Content-length The length of the request/response body, in bytes
Content-Encoding The encoding format of the request/response body, such as Gzip and Deflate
Accept-Encoding Tell them the content-encoding we accept
ETag Identifies the current resource, andLast-Modified,If-None-Match,If-Modified-SinceFor cache control
Cache-Control Generally, the value isno-cache,max-age=xx, xx is an integer, indicating the resource cache validity period (seconds)

Common request headers

The name of the role
Authorization Set the identity authentication information
User-Agent User id, such as OS and browser type and version
If-Modified-Since The value is returned by the previous serverLast-ModifiedValue to determine if a resource has been changed and read from the cache if it has not been changed
If-None-Match The value is the ETag value returned by the server last timeIf-Modified-Since
Cookie The existing cookies
Referer Identify the address from which the request is referenced, such as the address of page A when you jump from page A to page B
Host The requested host and port number

Request body

The request body (also called the request body) is a request parameter in the POST request mode. It is stored in the form of key = value, and multiple request parameters are connected with &. If the request body is in the request, the Content-Length attribute in the request header records the Length of the request body

A request message containing the request body

The request body of an HTTP request can take three different forms depending on the application scenario

The first:

As is common among mobile developers, the request body is of any type, the server will not parse the request body, and the processing of the request body will have to parse itself, such as when Posting JSON

HTTP request, form 1

The second:

The second and third have fixed formats and are the first to be known by server-side developers. The format requirement is the same as that of Query String in URL: Multiple key-value pairs are connected with &, and key-value pairs are connected with =. Only ASCII characters can be used. Non-ascii characters must be encoded with UrlEncode

HTTP request, form two

The third:

The third Type of request body is divided into multiple parts, which can be used for file uploading. This format is first used for email transmission. Each field/file is divided into separate sections by boundary (as specified in content-Type). Use a blank line after the description header and mark the end of the request with —

HTTP request, form three

The key to distinguishing content-disposition from being treated as a file is whether content-disposition contains filename. Because files have different types, content-type is also used to indicate the Type of file. Application /octet-stream if you don’t know what Type the file is, it’s a binary file. If it’s not a file, then the content-type can be omitted. Okay

HTTP response section

Except the status line on the format of the HTTP response (first line) with the request of the request message is not the same, the other in terms of format is the same, but excluding the status line and request the difference between, or from the Header can distinguish the difference between the HTTP request and HTTP response, how difference depends on the front of the Header

HTTP response packet

1. Response status line

Status code

If you want to see the specific meanings of various status codes, you can take a look at the HTTP status code comparison table in this article. Of course, it is difficult to remember all of these status codes.

That’s all we need to remember in normal times

Status code Corresponding information
1XX Info – The request is received and processing continues
2XX Indicates that the request has been successfully received, understood, or received
3XX Used to indicate that resources (web pages, etc.) are permanently transferred to another URL, also known as redirects
4XX Client error – The request has a syntax error or the request cannot be implemented
5XX Server side error – The server failed to fulfill a valid request

2. Response header

The response header can also be used to pass some additional information

Response headers

Common response headers

The name of the role
Date Date of the server
Last-Modified The time when the resource was last modified
Transfer-Encoding The value is generally chunked. When content-Length is not specified, it indicates that the server does not know the data size of the response boardContent-EncodingResponse headers
Set-Cookie Set the Cookie
Location Redirect to another URL, for example, enter browser, enter Baidu.com and press enter, it will automatically redirect towww.baidu.comIt’s controlled by this response header
Server Background server

3. Response body

The body of the response is the body Content of the web page. Generally, content-Length is used in the response header to specify the Length of the response body, which is easy for the browser to receive. Chunked encoding is also used for the body information with large amount of data.


Guess you like

  • Android a very simple, elegant diary APP
  • Android can save you a lot of detours
  • Android rolls up its sleeves and encapsulates dialog Fragments by itself
  • Teach you how to make a beautiful APP from scratch