table of Contents
HTTP protocol default port: 80
The HTTP protocol has two message formats:
2 Detailed explanation of Http protocol
2.1 Detailed explanation of Http request message
Http request message demo GET/POST
2.2 Detailed explanation of Http response message
1 Introduction to Http
What is the Http protocol
HTTP protocol: HyperText Transfer Protocol (HTTP, HyperText Transfer Protocol) is the most widely used network protocol on the Internet. It is used to define the process of data exchange between the WEB browser and the WEB server.
Transmission protocol: when the client and server communicate, the format of the transmission data is standardized
The role of HTTP protocol
The HTTP protocol is the cornerstone of learning Java WEB development. Without a thorough understanding of the HTTP protocol, one cannot say that one has mastered the development of WEB, and even less able to manage and maintain some complex WEB sites.
Features of HTTP protocol
Protocol based on request/response model. The request and response must be paired; there must be a request before a response is
simple and fast, because when sending a request, only the request method and request path are required to be
a stateless protocol. Multiple requests are independent of each other and cannot exchange data.
HTTP protocol default port: 80
For example: http://www.lagou.com:80
Http protocol version
HTTP/1.0, send a request, create a connection, obtain a web resource, and disconnect.
HTTP/1.1, send a request, create a connection, obtain multiple web resources, and disconnect.
The HTTP protocol has two message formats:
Request message: A message sent from the client to the server.
Response message: A message from the server to the client.
2 Detailed explanation of Http protocol
2.1 Detailed explanation of Http request message
HTTP request message: A message sent from the client to the server.
HTTP request message format: contains four parts : request line, request header, blank line, and request body (only in post mode)
Request line
Request line format: request method resource path protocol/version
For example: POST /web01/login.html HTTP/1.1
request line must be the first line of the HTTP request format.
Request method: There are 7 kinds of protocol stipulated, two commonly used: GET and POST
GET request:
It is not safe to append the request parameters to the URL. For example: login.html?username=tom&password=1234
URL length limits the data size of GET request.
No request body
POST request
request parameters are displayed in the request body, which is safer.
There is no limit to the size of the requested data.
Only when the form is set to method="post" is a post request. The others are get requests.
Common GET requests: direct access to the address bar, <a href=””>, <img src=””>, etc.
Request header
Request header: describes the HTTP protocol type used by the client to send a request to the server, the encoding used, and the length of the content sent, referer, etc.
The request header is also the key value pair key: value
Request body
Normally, the request body is used only in the post request method. The request body is the data submitted by the user form. Each item of data uses the key
value pair key=value, and multiple sets of values are connected by &.
For example; username=tom&password=1234
Http request message demo GET/POST
Create a page, write "login.html", and provide a form, and set the form submission methods to: get and post. Set the form submission
position to #, which means submit to the current form.
<form action="#" method="post">
用户名:<input type="text" name="username" value=""/> <br/>
密 码:<input type="text" name="password" value=""/> <br/>
<input type="submit" />
</form>
GET request to capture data:
POST request packet capture data:
2.2 Detailed explanation of Http response message
Response message: A message from the server to the client.
HTTP response message format: four parts: response line, response header, blank line, and response body
Response line
For example: HTTP/1.1 200 OK
Format: Protocol/Version Status Code Status Code Description
Status Code: The fixed number used by the server and browser to determine the status
200: The request is successful.
302: Request redirection.
304: The requested resource has not changed, and the local cache is accessed.
404: The requested resource does not exist. Usually the user path is written incorrectly, or the server resource may have been deleted.
500: Internal server error. Usually the program throws an exception.
Response header
Response header: Some descriptions used to describe the content returned by the server to the client browser, such as: what server am I, what code I return, how long the content I return, etc. The
response header is also a key-value pair key:value
Response body
The response body is the body that the server sends to the browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="#" method="get">
用户名: <input type="text" name="username" value=""/> <br/>
密 码: <input type="text" name="password" value=""/> <br/>
<input type="submit" />
</form>
</body>
</html>
Http response message demo
As shown in the figure below, we provide the response packet capture result (Chrome browser)