HTTP protocol, take you to understand the HTTP protocol

Table of contents

1. Introduction to HTTP protocol

2. Working process of HTTP protocol

The working process of the HTTP protocol can be divided into the following steps:

3. Introduction to Fiddler packet capture tool

3.1 Use of packet capture tools

3.2 Capture result

3.3 Principle of packet capture tool

4. Overview of HTTP protocol format

5. HTTP request (Request)

5.1 Understanding URLs

5.2 Understanding "method"

GET method:

POST method:

The difference between GET and POST:

Misconceptions about URL length issues with GET requests:

Misconceptions about POST being more secure than GET:

Misunderstandings about GET can only transfer text data:

5.3 Understanding the request "header" (header)

6. HTTP response (Response)

6.1 Understanding "status code" (status code)

6.2 Recognize the response "header" (header)

7. Construct HTTP request through form form

7.1 Send GET request

Summarize·


1. Introduction to HTTP protocol

HTTP (HyperText Transfer Protocol) is a protocol for transmitting hypertext, and it is one of the most widely used protocols on the Internet. The HTTP protocol works based on the client-server model. The client sends a request to the server, and the server returns a response. The HTTP protocol is stateless, which means that the server does not record the client's state information, and each request is independent.

2. Working process of HTTP protocol

The working process of the HTTP protocol can be divided into the following steps:

1. The client sends a request to the server.
2. After receiving the request, the server processes the request according to the requested URL and request method (GET, POST, etc.).
3. After processing the request, the server returns a response to the client.
4. After receiving the response, the client processes the response according to the response status code and response content.

3. Introduction to Fiddler packet capture tool

Fiddler is a free packet capture tool that can be used to capture HTTP and HTTPS traffic. Fiddler can help developers and testers analyze and debug network applications, and can also be used to view detailed information about network requests and responses.

3.1 Use of packet capture tools

Using the Fiddler packet capture tool can be divided into the following steps:

1. Download and install the Fiddler packet capture tool.
2. Open the Fiddler packet capture tool and click the "Start" button.
3. Visit the website that needs to capture packets in the browser.
4. View the packet capture results in the Fiddler tool.

3.2 Capture result

The Fiddler packet capture tool can display detailed information of requests and responses, including request method, URL, request header, request body, response status code, response header, response body, etc.

3.3 Principle of packet capture tool

The principle of the Fiddler packet capture tool is to capture network traffic through a proxy server. When the client sends a request, the request will first go through the Fiddler proxy server, and then forwarded to the target server. When the server returns a response, the response will also pass through the Fiddler proxy server before being returned to the client. Therefore, the Fiddler packet capture tool can capture all network traffic between the client and the server.

4. Overview of HTTP protocol format

The format of the HTTP protocol can be divided into request format and response format. The request format consists of request line, request header and request body, and the response format consists of status line, response header and response body.

Request format:


request line
request header
request body
 

Response format:


Status Line
Response Header
Response Body
 

5. HTTP request (Request)

An HTTP request consists of a request line, request headers, and a request body.

5.1 Understanding URLs

URL (Uniform Resource Locator) is an abbreviation for Uniform Resource Locator, which is used to identify resources on the Internet. A URL contains information such as protocol, hostname, port number, path, and query parameters.

Basic introduction to URL:


protocol://hostname:port number/path? query parameter
 

URL basic format:


http://www.example.com/index.html?name=Zhang San&age=18
 

Introduction to URL parameters:

URL parameters refer to the parameters passed through the query string in the URL, the format is "key=value", and multiple parameters are separated by "&" symbols.

Introduction to URLencode:

URLencode is an encoding method that converts special characters in URLs into ASCII codes, so that they can be transmitted in URLs. For example, a space will be converted to "%20", and Chinese will be converted to "%E4%BD%A0%E5%A5%BD".

5.2 Understanding "method"

The HTTP request method refers to the method used by the client to send a request to the server. The common methods are GET and POST.

GET method:

The GET method is used to request a specified page or resource and return a response. The request parameters of the GET method will be appended to the URL, separated by "?" symbols. For example:


GET /index.html?name=Zhang San&age=18 HTTP/1.1
Host: www.example.com
 

POST method:

The POST method is used to submit data to the server and return a response. The request parameters for the POST method are included in the request body rather than appended to the URL. For example:


POST /login HTTP/1.1
Host: www.example.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 23

name=Zhang San&age=18
 

The difference between GET and POST:

The difference between the GET method and the POST method lies in the way the request parameters are passed. The request parameters of the GET method will be appended to the URL, and the request parameters of the POST method will be included in the request body. Therefore, the GET method is suitable for transferring a small amount of data, while the POST method is suitable for transferring a large amount of data.

Misconceptions about URL length issues with GET requests:

Some people think that there is a limit to the length of URLs for GET requests, but this is actually a misconception. The HTTP protocol does not stipulate that there is a limit on the URL length of the GET request, but the browser and the server limit the URL length. Different browsers and servers have different restrictions on URL length. Generally speaking, URL length should not exceed 2048 characters.

Misconceptions about POST being more secure than GET:

Some people think that the POST method is more secure than the GET method, but in fact this is also a misconception. The POST method is the same as the GET method, both of which transmit data in plain text, but the transmission method of the request parameters is different. If data security needs to be ensured, the HTTPS protocol can be used to encrypt the transmitted data.

Misunderstandings about GET can only transfer text data:

Some people think that the GET method can only transfer text data, which is actually a misunderstanding. The GET method, like the POST method, can transmit any type of data, but the transmission method of the request parameters is different.

Other methods:

In addition to GET method and POST method, HTTP protocol also defines some other methods, such as PUT, DELETE, HEAD, OPTIONS and so on. The use scenarios of these methods are different, and you can choose to use them according to your specific needs.

5.3 Understanding the request "header" (header)

The HTTP request header refers to the information attached when the client sends a request to the server. Common request headers include Host, Content-Length, Content-Type, User-Agent, Referer, Cookie, etc.

Host:

The Host request header is used to specify the requested host name and port number in the format of "Host: host name: port number".

Content-Length:

The Content-Length request header is used to specify the length of the request body in the format of "Content-Length: Length".

Content-Type:

The Content-Type request header is used to specify the type of the request body. Common types include application/x-www-form-urlencoded, multipart/form-data, application/json, etc.

User-Agent (referred to as UA):

The User-Agent request header is used to specify the type and version number of the client. Common clients include browsers, crawlers, and mobile devices.

Refer:

The Referer request header is used to specify the source page of the request, and is often used for anti-leech and statistical analysis.

Cookie:

The Cookie request header is used to specify the session identifier of the client, which is often used to implement user login and session management.

6. HTTP response (Response)

An HTTP response consists of a status line, response headers, and a response body.

6.1 Understanding "status code" (status code)

The HTTP response status code refers to the status code attached when the server returns a response. Common status codes include 200 OK, 404 Not Found, 403 Forbidden, 405 Method Not Allowed, 500 Internal Server Error, 504 Gateway Timeout, 302 Move temporarily, 301 Moved Permanently et al.

200 OK:

200 OK indicates that the request was successful and the server returned the requested resource.

404 Not Found:

404 Not Found means that the requested resource does not exist.

403 Forbidden:

403 Forbidden means the request was rejected by the server, usually because the client does not have access rights.

405 Method Not Allowed:

405 Method Not Allowed indicates that the request method is not allowed, usually because the client uses an unsupported request method.

500 Internal Server Error:

500 Internal Server Error indicates an internal server error, usually because the server was unable to process the request.

504 Gateway Timeout:

504 Gateway Timeout indicates that the gateway timed out, usually because the server was unable to return a response within the specified time.

302 Move temporarily:

302 Move temporarily indicates that the requested resource has been temporarily moved to another URL.

301 Moved Permanently:

301 Moved Permanently indicates that the requested resource has been permanently moved to another URL.

6.2 Recognize the response "header" (header)

The HTTP response header refers to the information attached to the response returned by the server. Common response headers include Content-Type and so on.

Content-Type:

The Content-Type response header is used to specify the type of the response body. Common types include text/html, application/json, image/jpeg, etc.

7. Construct HTTP request through form form

7.1 Send GET request

Sending a GET request through the form form can use the following code:


<form action="http://www.example.com/index.html" method="get">
  <input type="text" name="name" value="张三">
  <input type="text" name="age" value="18">
  <input type="submit" value="提交">

Summarize·

This article mainly introduces the basic concepts and working process of the HTTP protocol, as well as the usage and principle of the Fiddler packet capture tool. In HTTP Requests, the basic format and parameters of URLs are introduced, as well as the differences and common misunderstandings between the GET and POST methods. At the same time, it also introduces common fields in the request header, such as Host, Content-Length, Content-Type, User-Agent, Referer, and Cookie. In HTTP responses, common status codes and the Content-Type field in the response header are introduced. Finally, this article also introduces how to construct HTTP requests through form forms and ajax, including the method of sending GET and POST requests. This article is detailed and suitable for beginners to understand the basic knowledge and usage of the HTTP protocol.

 

Guess you like

Origin blog.csdn.net/weixin_74021557/article/details/131211567