Getting started with HTTP protocol

Keywords: Java Spring intellij-idea

HTTP protocol

Chapter 1 Introduction to HTTP protocol

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-grbtid56-1637415789041) (Shang Silicon Valley Zhang Chunsheng _httpprotocol. assets/timg.jpg)]

  • HTTP hypertext transfer protocol is an object-oriented protocol belonging to the application layer. Because of its simple and fast way, it is suitable for distributed hypermedia information system. It was put forward in 1990. After more than ten years of use and development, it has been continuously improved and expanded. It is a data transfer protocol that specifies the rules of communication between browser and World Wide Web server and transmits World Wide Web documents through the Internet.

  • The content transmitted when the client communicates with the server is called a message. HTTP protocol specifies the format of message.

  • HTTP is a communication rule, which specifies the message format sent by the client to the server and the message format sent by the server to the client. In fact, we need to learn these two kinds of messages. What the client sends to the server is called "request message", and what the server sends to the client is called "response message".

  • Analogy to life cases: ① rent a house after graduation, sign a rental agreement, and standardize the rules to be observed by many parties. ② write a letter to a distant friend. Standardize the envelope.

    Actual Internet:

    • The client communicates with the server. For example, when a user accesses jd.com (which is a process of data transmission), the data transmission needs to be transmitted according to a protocol. For example, the user writes a letter to the server and the server replies to the user. There are formats: protocol. HTTP protocol specifies communication rules. It specifies how data is transmitted between the Internet.
      • Letter: message.
      • Write a letter: the user writes a letter to the server, and the user sends a request to the server. Send all the requested data and request messages
      • Reply: the server replies to the user and the browser. The server responds to all the data of the browser and responds to the message

Chapter 2 development of HTTP protocol

  • The predecessor of hypertext transfer protocol is Xanadu project. The concept of hypertext was put forward by Ted Nelson in the 1960s. After entering Harvard University, Nelson has been committed to the research of hypertext protocol and this project, but he has never publicly developed data. In 1989, Tim Berners Lee worked at CERN (European Organization for Nuclear Research) as a software consultant, he developed a set of programs, * * which laid the foundation of the world wide web (WWW = World Wide Web) * *. In December 1990, hypertext was first launched on CERN. In the summer of 1991, following Telnet and other protocols, hypertext transfer protocol became a part of many Internet protocols.

  • At that time, Telnet protocol solved the requirements of one-to-one controlled communication between one computer and another computer. Mail protocol solved the communication requirements of a sender sending information to a small number of people. File transfer protocol solved the communication requirements of a computer obtaining files in batches from another computer, but it did not have the ability to display files while obtaining files Or the function of processing files. The news transfer protocol solves the communication requirements of one to many news broadcasting. The communication requirements of hypertext are to obtain and display the text, data, pictures and other types of files stored in multiple computers on one computer; it includes two parts: Hypertext Transfer Protocol and hypertext markup language (HTML) The birth of HTTP, HTML and browser has brought a leap to the popularity of the Internet.

Chapter 3 session mode of HTTP protocol

  • The communication process between browser and server goes through four steps

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-sp9tsrvi-1637415789047) (Shang Silicon Valley Zhang Chunsheng _httpprotocol. assets/1557672342250.png)]

  • The connection process between the browser and the WEB server is short, and each connection only processes one request and response. For each page access, the browser and the WEB server should establish a separate connection.

  • All communications between the browser and the WEB server are completely independent and separate request and response pairs.

Chapter 4 differences between HTTP 1.0 and HTTP 1.1

In HTTP version 1.0, when the browser requests a web page with a picture, a new connection will be opened with the server due to downloading the picture; however, in HTTP version 1.1, the browser is allowed to disconnect after getting all the resources corresponding to the current request, which improves the efficiency.

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-gq3sjiq9-1637415789050) (Shang Silicon Valley Zhang Chunsheng _httpprotocol. assets/1557672415271.png)]

Chapter 5 monitoring HTTP operations by different browsers

5.1 IE8 browser: HttpWatch plug-in

  • Since IE browsers below IE8 do not provide the function of listening to HTTP, if you want to use browsers below IE8 to view the content of HTTP requests, you need to install a tool HttpWatch.

  • Both Firefox and chrome have built-in developer tools to view Http requests directly.

  • The use of HttpWatch is very simple. Install it directly, and then go to the next step until the installation is completed.

  • After installation, open IE browser and the HttpWatch Professional option can be seen from the tool drop-down list

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-uwz9ytkm-1637415789053) (Shang Silicon Valley Zhang Chunsheng _httpprotocol. assets/1557672511595.png)]

  • After opening, click the Record button to start listening for Http requests.

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (IMG oregkthm-1637415789055) (Shang Silicon Valley Zhang Chunsheng _httpprotocol. assets/1557672538558.png)]

5.2 viewable by F12 in IE9 browser

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-q9cmjczy-1637415789058) (Shang Silicon Valley Zhang Chunsheng _httpprotocol. assets/1561904433377.png)]

5.3 it can be viewed with F12 in chrome

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-rowzek6h-1637415789061) (Shang Silicon Valley Zhang Chunsheng _httpprotocol. assets/1561904121561.png)]

5.4 F12 in Firefox

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-zyhjageg-1637415789064) (Shang Silicon Valley Zhang Chunsheng _httpprotocol. assets/1561904179928.png)]

Chapter 6 message

6.1 message format

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-cenc5d8f-1637415789067) (Shang Silicon Valley Zhang Chunsheng _httpprotocol. assets/1557672592385.png)]

  • Message:
    • Request message: the browser sends it to the server

    • Response message: the server sends it back to the browser

6.2 request message

6.2.1 message format (4 parts)

  • The first line of the request (request line);

  • Request header information (request header);

  • Blank line;

  • Request body;

6.2.2 GET request

1. Since the request parameters have been carried in the first line of the request, there is no request body and no empty line
2. The request parameters are spliced in the URL address, and [url? Name1 = value1 & Name2 = Value2] can be seen in the address bar, which is unsafe
3. Since the parameters are carried in the address bar, they are limited by size [the data size of the address bar is generally limited to 4k], and can only carry plain text
4. get request parameters can only upload text data
5. No request body. Therefore, the encapsulation and parsing are fast and efficient. By default, the requests submitted by the browser are get requests [for example, ① enter the url address in the address bar, ② click hyperlink a, ③ the default form method...]

  • Request first line
GET /05_web_tomcat/login_success.html?username=admin&password=123213 HTTP/1.1

Request mode 	The resource path in the accessed server? get Request parameters	Protocol version
  • Request header
Host: localhost:8080   Host virtual address
Connection: keep-alive Long connection
Upgrade-Insecure-Requests: 1  Request automatic upgrade of protocol[http Request, server is https , the browser will automatically upgrade the request protocol to https of]
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36
- User system information
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
- Browser supported file types
Referer: http://localhost:8080/05_web_tomcat/login.html
- The path of the previous page of the current page[Which page does the current page Jump from]:    You can jump back to the previous page through this path to charge advertising and prevent chain theft
Accept-Encoding: gzip, deflate, br
- Browser supported compression formats
Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7
- Browser supported languages

6.2.3 POST request

  • The post request requires that the method property of the form tag be set to post

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-9nuedxld-1637415789069) (Shang Silicon Valley Zhang Chunsheng _httpprotocol. assets/1557672877007.png)]

1. The post request has a request body, while the GET request has no request body.
2. The post request data is carried in the request body. The size of the request body data is unlimited and can be used to upload all content [files, text]
3. Only post requests can be used to upload files
4. The post request message has more configuration [request header] related to the request body
5. Address bar parameters are invisible and relatively safe
6. post is less efficient than get

  • Request first line
POST /05_web_tomcat/login_success.html HTTP/1.1
  • Request header
Host: localhost:8080
Connection: keep-alive
Content-Length: 31 		-Length of request body content
Cache-Control: max-age=0  -No cache
Origin: http://localhost:8080
Upgrade-Insecure-Requests: 1  -Automatic upgrade of protocol
Content-Type: application/x-www-form-urlencoded   -Request body content type[The server resolves the request body parameters according to the type]
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: http://localhost:8080/05_web_tomcat/login.html
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7
Cookie:JSESSIONID-
  • Request blank line
  • Request body: the content submitted by the browser to the server
username=admin&password=1232131

6.3 response message

6.3.1 message format (4 parts)

  • First line of response (response line);

  • Response header information (echo header);

  • Blank line;

  • Responder;

6.3.2 specific conditions

  • First line of response:

    HTTP/1.1 200 OK
    
    Note: the response protocol is HTTP1.1,The response status code is 200, indicating that the request is successful; 
    
  • Response header:

    Server: Apache-Coyote/1.1   Server version information
    Accept-Ranges: bytes
    ETag: W/"157-1534126125811"
    Last-Modified: Mon, 13 Aug 2018 02:08:45 GMT
    Content-Type: text/html    Type of response body data[The browser parses the response body data according to the type]
    Content-Length: 157   Number of bytes of response body content
    Date: Mon, 13 Aug 2018 02:47:57 GMT  Response time, which may have a time zone difference of 8 hours
    
  • Response blank line

  • Responder

    <!--Content to be parsed by browser[If the response is html Page, the final response body content will be displayed in the page by the browser]-->
    
    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<title>Insert title here</title>
    	</head>
    	<body>
    		Congratulations, login succeeded...
    	</body>
    </html>
    

6.3.3 response code

The response code is very important to the browser. It tells the browser the result of the response. The representative response codes are as follows:

  • **200: * * if the request is successful, the browser will display the content of the response body (usually html) in the browser;

  • **404: * * the requested resource is not found, which indicates that the client incorrectly requested a nonexistent resource;

    [the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-khsko73r-1637415789072) (Shang Silicon Valley Zhang Chunsheng _httpprotocol. assets/1561905288592.png)]

    [the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-e5gx4pxi-1637415789074) (Shang Silicon Valley Zhang Chunsheng _httpprotocol. assets/1561905338054.png)]

    [the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-tobssjug-1637415789076) (Shang Silicon Valley Zhang Chunsheng _httpprotocol. assets/1561905371728.png)]

  • **500: * * the requested resource was found, but an internal error occurred in the server;

    [the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-vg6nvhj7-1637415789078) (Shang Silicon Valley Zhang Chunsheng _httpprotocol. assets/1561905801690.png)]

    [the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-odnozzol-1637415789080) (Shang Silicon Valley Zhang Chunsheng _httpprotocol. assets/1561905834066.png)]

  • **302: * * redirection. When the response code is 302, it means that the server requires the browser to resend a request, and the server will send a response header Location, which specifies the URL address of the new request;

In addition, some other response codes are as follows:

200 - The server returned the web page successfully 
404 - The requested page does not exist 
503 - Service Unavailable 
Detailed breakdown:

1xx((temporary response) 
A status code that represents a temporary response and requires the requester to continue the operation.

Code description 
100 ((Continued) the requester shall continue to make a request. The server returns this code to indicate that it has received the first part of the request and is waiting for the rest. 
101 (Handover protocol) the requester has asked the server to switch the protocol, and the server has confirmed and is ready to switch.

2xx ((successful) 
Status code indicating that the request was successfully processed.

Code description 
200 (Success) the server has successfully processed the request. Typically, this means that the server has provided the requested web page. 
201 (The request succeeded and the server created a new resource. 
202 (Accepted) the server accepted the request but has not yet processed it. 
203 (Unauthorized information) the server has successfully processed the request, but the information returned may be from another source. 
204 (No content) the server successfully processed the request, but did not return any content. 
205 (Reset content) the server successfully processed the request but returned nothing. 
206 (Partial content) the server successfully processed partial content GET Request.

3xx (Redirect) 
Indicates that further action is required to complete the request. Typically, these status codes are used for redirection.

Code description 
300 (Multiple choices) the server can perform a variety of operations for requests. The server can be based on the requester (user agent) Select an action or provide a list of actions for the requester to select. 
301 (Permanent move) the requested page has been permanently moved to a new location. The server returned this response (yes) GET or HEAD The requester is automatically transferred to a new location when the request is received. 
302 (The temporary mobile) server currently responds to requests from web pages in different locations, but the requester should continue to use the original location for future requests. 
303 (View other locations) requesters should use separate for different locations GET The server returns this code when a request is made to retrieve a response. 
304 ((not modified) the requested page has not been modified since the last request. When the server returns this response, the web page content will not be returned. 
305 (Using a proxy) a requester can only use a proxy to access the requested web page. If the server returns this response, it also indicates that the requester should use a proxy. 
307 (Temporary redirection) the server currently responds to requests from web pages in different locations, but the requester should continue to use the original location for future requests.

4xx(Request error) 
These status codes indicate that the request may be in error and hinder the processing of the server.

Code description 
400 (Error (request) the server does not understand the syntax of the request. 
401 (Unauthorized) request requires authentication. The server may return this response for web pages that require login. 
403 ((Forbidden) the server rejects the request. 
404 ((not found) the server could not find the requested page. 
405 (Method disable) disables the method specified in the request. 
406 ((not accepted) cannot respond to the requested page with the requested content attribute. 
407 (Proxy authorization required) this status code is similar to 401 (unauthorized), but specifies that the requester should authorize the use of the proxy. 
408 (Request timeout) a timeout occurred while the server was waiting for a request. 
409 (Conflict) the server encountered a conflict while completing the request. The server must include information about the conflict in the response. 
410 (Deleted) the server returns this response if the requested resource has been permanently deleted. 
411 (A valid length is required) the server does not accept requests without a valid content length header field. 
412 (Prerequisites not met) the server does not meet one of the prerequisites set by the requester in the request. 
413 (The request entity is too large) the server cannot process the request because the request entity is too large to handle the server. 
414 (Requested URI (too long) requested URI(Usually the web address) is too long for the server to handle. 
415 (Unsupported media type) the requested format is not supported by the requested page. 
416 (The requested range does not meet the requirements) if the page cannot provide the requested range, the server returns this status code. 
417 (Not met) the server did not meet the requirements for the expected request header field.

5xx(Server error) 
These status codes indicate that the server encountered an internal error while trying to process the request. These errors may be the server itself, not the request.

Code description 
500 (Server internal error) the server encountered an error and was unable to complete the request. 
501 ((not implemented yet) the server does not have the ability to complete the request. For example, this code may be returned when the server does not recognize the requested method. 
502 (Error: the server, as a gateway or proxy, received an invalid response from the upstream server. 
503 (Service unavailable) the server is currently unavailable (due to overload or downtime maintenance). Usually, this is only temporary. 
504 (Gateway timeout) the server acts as a gateway or proxy, but does not receive the request from the upstream server in time. 
505 (HTTP Version not supported) the server does not support the version used in the request HTTP Protocol version.

HttpWatch Status code Result is

200 - The server successfully returned the web page, and the client request was successful. 
302 - Objects are temporarily moved. At present, the server responds to requests from web pages in different locations, but the requester should continue to use the original location for future requests. 
304 - Belongs to redirection. The requested page has not been modified since the last request. When the server returns this response, the web page content will not be returned. 
401 - Unauthorized. The request requires authentication. The server may return this response for web pages that require login. 
404 - Not found. The server could not find the requested page. 
2xx - success. Indicates that the server successfully accepted the client request. 
3xx - Redirect. Indicates that further action is required to complete the request. The client browser must do more to implement the request. For example, the browser may have to request a different page on the server or repeat the request through the proxy server. 
4xx - Request error. These status codes indicate that the request may be in error and hinder the processing of the server. 
5xx - Server error. Indicates that the server encountered an internal error while trying to process the request. These errors may be the server itself, not the request.

6.3.4 MIME type (understand)

HTTP content type: the data type of the current response body.

① It should be noted that the data types transmitted between the browser and the server are not all text types, but also multimedia types such as pictures, video, audio and so on. These multimedia types are defined using MIME types.

② The full English name of mime is "Multipurpose Internet Mail Extensions" multifunctional Internet mail extension service. The format of MIME type is "large type / small type" and corresponds to the extension of a file.

③ Common MIME types

fileMIME type
Hypertext markup language text.html,.htm text/html
Plain text.txt text/plain
Rich text.rtf application/rtf
GIF graphics.gif image/gif
JPEG graphics.jpeg,.jpg image/jpeg
au sound file.au audio/basic
MIDI music filesmid,.midi audio/midi,audio/x-midi
RealAudio music files.ra, .ram audio/x-pn-realaudio
MPEG file .mpg,.mpeg video/mpeg
AVI file.avi video/x-msvideo
GZIP file.gz application/x-gzip
TAR file.tar application/x-tar

task

  |

|MIDI music files | mid,.midi audio/midi,audio/x-midi|
|RealAudio music files,. RA,. Ram audio / x-pn-realaudio|
|MPEG files,. mpg,.mpeg video/mpeg|
|AVI file. avi video/x-msvideo|
|GZIP file |. gz application/x-gzip|
|TAR file |. tar application/x-tar|

task

Be familiar with the main formats of HTTP requests and responses

Posted by phpsharma on Mon, 22 Nov 2021 02:28:51 -0800