Features of Http protocol

Features of HTTP protocol

  • Protocol based on request/response model.

    • Requests and responses must be paired;
    • A request comes before a response.
  • Simple and quick

    • Because when you send a request, you only need to send the request mode and the request path
  • The default HTTP port is 80

    • Such as:http://www.baidu.com:80

Version of the Http protocol

  • HTTP/1.0, send a request, create a connection, get a Web resource, and disconnect.
  • HTTP/1.1, sends a request, creates a connection, obtains multiple Web resources, and disconnects.

The HTTP protocol has two packet formats:

  • Request packet: a packet sent by the client to the server.
  • Response packet: packet sent from the server to the client.

Http protocol details

Description of Http request packets

HTTP request packets: packets sent from the client to the server.

HTTP request packet format: contains the request line, request header, blank line, and request body

The request line

For example: POST /web01/login. HTML HTTP/1.1 request line must be the first line in the HTTP request format. Format of the request line: Request Mode Resource Path Protocol/Version Request mode: Seven protocols are available. Two commonly used methods are as follows: GET and POST GET request: Apends request parameters to the URL, which is unsafe. For example: the login HTML? Username = tom&Password =1234 URL Length Limits the size of data in GET request mode. No request body POST request parameters display the request body, which is safer. Requested data size not displayed. Post requests are made only if the form is set to method= "POST". Everything else is a GET request. <a href= "" > <img SRC =" "Copy the code

Request header

Request header: Describes the HTTP protocol type used by the client to send the request to the server, the encoding used, the length of the content sent, the referer, and so on.

The request header also uses key:value pairs

Common request headers describe
Referer The browser informs the server where the current request is coming from. If you are accessing directly, you will not have this header. Often used for: anti-theft chain
Cookie Session-related technology used to store cookie information cached by the browser.
User-Agent The browser notifies the server, the client browser, and the operating system

Request body

Normally, only post requests are used in the request body. The request body contains the data submitted by the user from the form. Each item of data uses the key=value pair, and multiple groups of values are connected by &. For example; username=tom&password=1234Copy the code

Demonstration of Http request packets

  • Create a page, write “login. HTML”, and provide a form. Set the form submission modes to GET and POST respectively. Set the form submission position to # to indicate submission to the current form.

<form action="#" method="post">User name:<input type="text" name="username" value=""/> <br/>Password:<input type="text" name="password" value=""/> <br/>
    <input type="submit" />
</form>
Copy the code

The following figure shows the result of packet capture using GET request and POST request :(Chrome)

  • GET Requests packet capture data:

  • POST Requests packet capture data:

2.2 Details of Http Response Packets

Response packet: packet sent from the server to the client.

HTTP response packet format: 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: a 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, accessing the local cache. 404: Requested resource does not exist. Typically, the user path was written incorrectly, or perhaps the server resource was deleted. 500: The server has an internal error. Usually the program throws an exception.Copy the code

Response headers

Response header: A description of the content the server sends back to the client browser, such as: what server am I, what encoding am I returning, how long is the content I am returning, etc.

The response header also uses key:value pairs

Common request headers describe
Location The response path must be used together with the status code 302 to complete the jump.
Content-Disposition Used when downloading files. Parse the text as a download through the browser
Set-Cookie Session-related technologies. The server writes cookies to the browser
Refresh Time to refresh

Response body

The response body is the body that the server sends to the browser.

<! DOCTYPEhtml>
<html >
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="#" method="post">User name:<input type="text" name="username" value=""/> <br/>Password:<input type="text" name="password" value=""/> <br/>
        <input type="submit"/>
    </form>
</body>
</html>
Copy the code

Http response packet demonstration

As shown below, we provide the packet capture result of the response (Chrome)