This is the first article I participated in beginners’ introduction.

What is the HTTP protocol

  • The HTTP protocol (Hypertext Transfer Protocol.Hypertext transfer protocol) specifications for reliable transmission of text, pictures, audio, video and other hypertext data between the client and the server.
  • Http belongs to the application layer and is the first layer for users to access.

HTTP protocol format

The so-called hypertext Transfer protocol (HYPERtext Transfer Protocol) is the transmitted text information, which has certain restrictions on the format of the text information.

Request message format

HTTP messages consist of requests from the client to the server and responses from the server to the client. The format of the request packet is as follows:

Request line – General information Header – Request Header – Entity Header – Message body

The request message is as follows

POST https://api.juejin.cn/interact_api/v1/comment/list HTTP / 1.1 Accept - Encoding: gzip, deflate the content-type: application/json Content-Length: 90 Host: api.juejin.cn Connection: Keep-Alive User-Agent: The Apache HttpClient / 4.4.1 (Java 1.5) {"item_id":"6981086026055811079","item_type":2,"cursor":"0","limit":20,"client_type":2608}Copy the code
  1. The request line

POST indicates the request type, api.juejin.cn/interact_ap… For the resource to access, HTTP/1.1 is the protocol version

  1. The request header

From the second line is the request header, accept-encoding indicates the request format; Content-type refers to the data Type requested; Content-length Specifies the Length of the request format. Host indicates the destination of the request (Host domain name); Connection Connection mode; User-agent This is important information that detects the browser type, is defined by the browser, and is sent automatically with each request.

  1. A blank line

The request header must be followed by an empty line

  1. The request data

The requested data, also called the request body, can be added to any other data.

Response message format

The reply packet format is as follows:

Status line – General message Header – Response Header – Entity Header – Message body

HTTP/1.1 200 OK
Server: Tengine
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Date: Tue, 06 Jul 2021 15:05:02 GMT
Vary: Accept-Encoding
X-Tt-Logid: 20210706230502010212150152150DC272
server-timing: inner; dur=47
x-tt-trace-host: 0105f83323851c44e692c39c28f85b3e51f77f4f44ef560dbb5ebeed4d00e2ad7e5389222aad8f43cc8a4e29c81a8d3c58f50d8995db9649cf4329ac8991656c5bbf8c9beb6a1cc49bec51a253adafea6b5f41ab3f1638949caafec97204337eeb
x-tt-trace-tag: id=3;cdn-cache=miss
X-TT-TIMESTAMP: 1625583902.666
Content-Encoding: gzip
Via: cache3.l2cn1800[88,0], cache6.cn1756[133,0]
Timing-Allow-Origin: *
EagleId: 7030991a16255839025774966e

{"err_no":0,"err_msg":"success","data":[{"comment_id":"6981339904140051213","comment_info":{"comment_id":"6981339904140051213","user_id":"4169821261664952","item_id":"6981086026055811079","item_type":2,"comment_content":"婊存淮婊存淮","comment_pics":[],"comment_status":1,"ctime":1625469867,"comment_replys":[],"digg_count":0,"bury_count":0,"reply_count":0,"is_digg":false,"is_bury":false,"level":0},"user_info":{"user_id":"4169821261664952","user_name":"骞插暐鐜╂剰","company":"","job_title":"","avatar_large":"https://sf3-ttcdn-tos.pstatp.com/img/mosaic-legacy/3792/5112637127~300x300.image","level":0,"description":"","followee_count":1,"follower_count":0,"post_article_count":0,"digg_article_count":3,"got_digg_count":0,"got_view_count":0,"post_shortmsg_count":0,"digg_shortmsg_count":0,"isfollowed":false,"favorable_author":0,"power":0,"study_point":0,"university":{"university_id":"0","name":"","logo":""},"major":{"major_id":"0","parent_id":"0","name":""},"student_status":0,"select_event_count":0,"select_online_course_count":0,"identity":0,"is_select_annual":false,"select_annual_rank":0,"annual_list_type":0,"extraMap":{},"is_logout":0},"user_interact":{"id":6981086026055811079,"omitempty":2,"user_id":0,"is_digg":false,"is_follow":false,"is_collect":false},"reply_infos":[],"is_author":false},{"comment_id":"6981339028272972581","comment_info":{"comment_id":"6981339028272972581","user_id":"3069492197591624","item_id":"6981086026055811079","item_type":2,"comment_content":"鍐插啿chong","comment_pics":[],"comment_status":1,"ctime":1625469664,"comment_replys":[],"digg_count":0,"bury_count":0,"reply_count":0,"is_digg":false,"is_bury":false,"level":0},"user_info":{"user_id":"3069492197591624","user_name":"鍙虫墜鎷夋媺閾�,"company":"","job_title":"","avatar_large":"https://sf1-ttcdn-tos.pstatp.com/img/user-avatar/d52827434789fbb81c8f78169e48f87c~300x300.image","level":0,"description":"","followee_count":5,"follower_count":1,"post_article_count":1,"digg_article_count":6,"got_digg_count":0,"got_view_count":31,"post_shortmsg_count":0,"digg_shortmsg_count":0,"isfollowed":false,"favorable_author":0,"power":0,"study_point":0,"university":{"university_id":"0","name":"","logo":""},"major":{"major_id":"0","parent_id":"0","name":""},"student_status":0,"select_event_count":0,"select_online_course_count":0,"identity":0,"is_select_annual":false,"select_annual_rank":0,"annual_list_type":0,"extraMap":{},"is_logout":0},"user_interact":{"id":6981086026055811079,"omitempty":2,"user_id":0,"is_digg":false,"is_follow":false,"is_collect":false},"reply_infos":[],"is_author":false},{"comment_id":"6981287607942513415","comment_info":{"comment_id":"6981287607942513415","user_id":"1425421481546093","item_id":"6981086026055811079","item_type":2,"comment_content":"鍏堟墦涓崱馃槂","comment_pics":[],"comment_status":1,"ctime":1625457690,"comment_replys":[],"digg_count":0,"bury_count":0,"reply_count":0,"is_digg":false,"is_bury":false,"level":0},"user_info":{"user_id":"1425421481546093","user_name":"瀹囨櫤娉娲惧ぇ鏄�,"company":"","job_title":"鍓嶇鏂版墜","avatar_large":"https://sf6-ttcdn-tos.pstatp.com/img/user-avatar/84991613df03e46dad8d34ddef4605f6~300x300.image","level":0,"description":"","followee_count":9,"follower_count":2,"post_article_count":0,"digg_article_count":1,"got_digg_count":0,"got_view_count":0,"post_shortmsg_count":7,"digg_shortmsg_count":19,"isfollowed":false,"favorable_author":0,"power":0,"study_point":0,"university":{"university_id":"0","name":"","logo":""},"major":{"major_id":"0","parent_id":"0","name":""},"student_status":0,"select_event_count":0,"select_online_course_count":0,"identity":0,"is_select_annual":false,"select_annual_rank":0,"annual_list_type":0,"extraMap":{},"is_logout":0},"user_interact":{"id":6981086026055811079,"omitempty":2,"user_id":0,"is_digg":false,"is_follow":false,"is_collect":false},"reply_infos":[],"is_author":false},{"comment_id":"6981245195145151245","comment_info":{"comment_id":"6981245195145151245","user_id":"34801414070510","item_id":"6981086026055811079","item_type":2,"comment_content":"鍏堟墦涓崱馃槂","comment_pics":[],"comment_status":1,"ctime":1625447816,"comment_replys":[],"digg_count":0,"bury_count":0,"reply_count":0,"is_digg":false,"is_bury":false,"level":0},"user_info":{"user_id":"34801414070510","user_name":"Jason涓嶅緱","company":"","job_title":"","avatar_large":"https://sf3-ttcdn-tos.pstatp.com/img/user-avatar/b5768337c7c60304aea5c62aeba221c3~300x300.image","level":0,"description":"","followee_count":2,"follower_count":0,"post_article_count":1,"digg_article_count":3,"got_digg_count":0,"got_view_count":34,"post_shortmsg_count":1,"digg_shortmsg_count":2,"isfollowed":false,"favorable_author":0,"power":0,"study_point":0,"university":{"university_id":"0","name":"","logo":""},"major":{"major_id":"0","parent_id":"0","name":""},"student_status":0,"select_event_count":0,"select_online_course_count":0,"identity":0,"is_select_annual":false,"select_annual_rank":0,"annual_list_type":0,"extraMap":{},"is_logout":0},"user_interact":{"id":6981086026055811079,"omitempty":2,"user_id":0,"is_digg":false,"is_follow":false,"is_collect":false},"reply_infos":[],"is_author":false}],"cursor":"4","count":4,"has_more":false}
Copy the code
  1. The status line

The status line consists of the protocol version number, status code, and status message

  1. Response headers

The response header is information that the client can use, such as Date (the Date on which the response was generated), Content-Type (MIME Type and encoding format), Connection (long Connection by default), and so on

  1. A blank line

There must be a blank line between the response header and the response body

  1. Response body

The response body, in this case the key-value pair information

How many steps does a complete HTTP request go through?

HTTP communication mechanism In a complete HTTP communication process, the Web browser and the Web server will complete the following seven steps:

  1. Establishing a TCP Connection
  2. The Web browser sends a request line to the Web server
  3. The Web browser sends the request header
  4. Web Server Reply
  5. The Web server sends the reply header
  6. The Web server sends data to the browser
  7. The Web server closes the TCP connection

4. Response status code

The HTTP status code consists of three digits. The first digit defines the category of the response. There are five categories:

  • 1.1 XX: Indicating message — indicates that the request has been received and processing continues
  • 2.2xx: Success: Indicates that the request is successfully received, understood, and accepted
  • 3.3xx: Redirection – Further action must be taken to complete the request
  • 4.4 XX: Client error: The request has a syntax error or cannot be implemented
  • 5.5 XX: Server side error — The server failed to fulfill a valid request
  • Common status codes:
401 Unauthorized // The Request is Unauthorized. This status code must be used with the www-Authenticate header field using 403 Forbidden // The server received the request but refused to provide the service 404 Not Found // The requested resource did Not exist, eg: An incorrect URL is entered. 500 Internal Server Error // An unexpected Error occurs on the Server. 503 Server Unavailable // The Server cannot process requests from the clientCopy the code

Five, Http protocol has those requests

HTTP defines a variety of request methods to meet a variety of requirements. HTTP/1.0 defined three request methods: GET, POST, and HEAD. HTTP/1.1 added five request methods: OPTIONS, PUT, DELETE, TRACE, and CONNECT. The specific functions of each request method are as follows:

GET requests the specified page information and returns the entity body. A HEAD is similar to a GET request, except that there is no concrete content in the response returned. It is used to retrieve the header and POST to submit data to a specified resource for processing the request (such as submitting a form or uploading a file). The data is contained in the request body. POST requests may result in the creation of new resources and/or the modification of existing resources. PUT Transmits data from the client to the server instead of the content of the specified document. DELETE requests the server to DELETE the specified page. CONNECT Reserved in HTTP/1.1 for proxy servers that can pipe connections. OPTIONS allows clients to view server performance. TRACE displays the requests received by the server for testing or diagnosis.Copy the code

The difference between GET and POST methods

  • Difference 1: Get obtains resources from the server, and POST sends data to the server.
  • Difference 2: Get transmits a small amount of data because it is limited by URL length, but it is efficient. Post transfers a large amount of data. Therefore, you can upload files only in Post mode.
  • Get is not secure, because get requests to send data on the URL, is visible, may reveal private information, such as passwords, etc. Post is placed at the head of the request and is safe.

How HTTPS works

  • First, the HTTP request server generates a certificate. The client verifies the validity period and validity of the certificate, whether the domain name is consistent with the requested domain name, and the public key (RSA encryption) of the certificate.
  • 2. If the verification succeeds, the client generates a random number based on the validity of the public key of the certificate. The random number is encrypted using the public key (RSA encryption).
  • Thirdly, after the message body is generated, the digest of the message is encrypted by MD5 (or SHA1) algorithm, and then the RSA signature is obtained.
  • 4. Send it to the server. Only the server (RSA private key) can decrypt it.
  • 5. Decrypt the random number, encrypt it with AES, and use it as the key (only the client and server know the key at this time).

Seven,

HTTP requests are not as complex as we think. The server interacts with the browser to transfer text information according to certain rules (HTTP protocol). Understand what the hypertext protocol is, and don’t get confused when you request an interface during development.