The computer network part of the front-end interview (1) 10 questions each

1. The difference between GET and POST requests

GET and POST are two common request methods in the HTTP protocol. They have the following differences in sending requests and transmitting data:

  1. request location :

    • GET: Send a request to the server through a URL (Uniform Resource Locator), parameters and data are appended to the query string of the URL, for example:https://example.com/api/data?id=123
    • POST: Put the requested parameters and data in the message body of the request instead of the URL.
  2. Request length :

    • GET: Since the data is appended to the URL, the length of the URL is limited, and it is not suitable for transmitting large amounts of data. It is usually used to obtain resources or query data.
    • POST: Put the data in the request message body, there is no limit on the length of the URL, suitable for transmitting large amounts of data, usually used to submit forms or send complex data.
  3. Request Security :

    • GET: Because parameters and data are exposed in the URL, it is not suitable for sending sensitive information, because they may be recorded by cache, browser history or server logs, etc.
    • POST: Since the data is in the request message body, it is relatively safer and suitable for sending sensitive information.
  4. Request semantics :

    • GET: Indicates a request to obtain a specified resource. Executing a GET request should have no side effects, that is, it should not affect data on the server.
    • POST: Indicates submitting data to the server, which may lead to changes in the server state, because POST requests are used to create, update, or delete resources.
  5. request cache :

    • GET: It is usually cached by the browser. If the same URL and parameters are requested multiple times, the browser may directly obtain the result from the cache to improve performance.
    • POST: Usually not cached, every time a POST request is sent, the data will be re-requested from the server.

To sum up, GET and POST are HTTP request methods. GET is suitable for obtaining resources and querying data. The data is appended to the URL and has a limited length, so it is not suitable for transmitting sensitive information; while POST is suitable for submitting data and changing the server status. In the request message body, it is suitable for transmitting large amounts of data and sensitive information. Developers should choose an appropriate request method based on actual needs and security considerations.

2. The difference between POST and PUT requests

POST and PUT are two common request methods in the HTTP protocol. They have the following differences in sending requests and processing data:

  1. Request semantics :

    • POST: Indicates submitting data to the server to create new resources or perform certain operations. Each request may have side effects, that is, to change the data state of the server. POST requests are often used to submit forms, upload files, perform transactions, and other operations.
    • PUT: Indicates transferring data to the server to update or replace all the contents of the specified resource. A PUT request requires the client to provide the full resource content, not a partial update.
  2. Idempotence :

    • POST: POST requests are not idempotent, submitting the same data multiple times may result in repeated creation of server resources or performing the same operation multiple times.
    • PUT: PUT requests are idempotent. Submitting the same data multiple times has no effect on the resource status of the server. No matter how many times the request is executed, the result is the same.
  3. Data processing :

    • POST: Usually the requested data is placed in the message body of the request, and the data processing is more flexible and can be processed according to different business scenarios.
    • PUT: The PUT request also puts data in the message body of the request, but requires the client to provide complete resource content, that is, completely replace the old resource content with new data.
  4. Resource URIs :

    • POST: Usually the URI of the resource is not specified in the request, and the URI of the resource is automatically generated by the server. POST requests are used to create new resources, so the URI of the resource is determined by the server.
    • PUT: The PUT request must specify the full URI of the resource and is used to update the content of the specified resource.
  5. Request processing semantics :

    • POST: POST requests have no specific semantics, and the server processes requests according to business logic.
    • PUT: The PUT request has clear semantics, that is, the content of the specified resource is replaced with the data provided in the request.

In general, both POST and PUT are HTTP request methods, POST is used to create a new resource or perform an operation, and PUT is used to update or replace the entire content of a specified resource. POST requests are not idempotent and may cause side effects, while PUT requests are idempotent, and multiple requests will not affect resource status. Developers should choose an appropriate request method based on business requirements and resource processing logic.

3. Common HTTP request headers and response headers

HTTP request headers and response headers are a way of passing information during HTTP requests and responses. They contain some key-value pairs, which are used to describe some additional information and parameters of the request or response. The following are common HTTP request headers and response headers:

Common HTTP request headers:

  1. Host : Specify the host name and port number of the target server.
  2. User-Agent : Identifies the user agent (browser or other client application) information that initiated the request.
  3. Accept : Specifies the content type accepted by the client, for example: Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8indicates that the client can accept resources in HTML, XML, and WebP formats.
  4. Content-Type : Specifies the MIME type of the request body, which is used to inform the server of the format of the request body, for example: Content-Type: application/jsonindicating that the request body is data in JSON format.
  5. Authorization : Used to carry authentication information, for example: the Authorization header of Basic Authentication will contain the Base64 encoding of the username and password.
  6. Cookie : Used to carry HTTP Cookie in the request, and bring the Cookie value sent by the server back to the server.
  7. Referer : contains the URL of the source page of the currently requested page, used to identify the source of the request.

Common HTTP response headers:

  1. Content-Type : Specifies the MIME type of the response body, which is used to inform the client of the format of the response body, for example: Content-Type: text/html; charset=utf-8indicating that the response body is UTF-8 encoded HTML.
  2. Content-Length : Specifies the length of the response body, which is used to inform the client of the size of the response body.
  3. Set-Cookie : Used to set HTTP Cookie in the response and save the Cookie value on the client.
  4. Cache-Control : It is used to specify the caching strategy of the response, for example: Cache-Control: max-age=3600it means that the response can be cached for 3600 seconds.
  5. Location : Used for redirection responses, including the redirected URL.
  6. Server : Specifies the server information of the response, which is used to tell the client which server generated the response.
  7. Access-Control-Allow-Origin : Used to support cross-domain requests, specifying domains that are allowed to access the response.

The above are only part of the HTTP request header and response header. The HTTP header is very flexible and can be customized as needed. They play an important role in the HTTP communication process, providing various metadata and configuration information to help clients and servers communicate and coordinate.

4. Common HTTP request methods

HTTP request methods refer to different types of request operations defined in the HTTP protocol, which are used to inform the server of operations to be performed. Common HTTP request methods include:

  1. GET : Used to request information about a specified resource. GET requests should be idempotent, that is, they have no impact on server resources, and the results of multiple requests are the same. Parameters for GET requests are usually appended to the URL's query string.

  2. POST : used to submit data to the server, used to create new resources or perform certain operations. POST requests may have an impact on the server's data state and are therefore not idempotent.

  3. PUT : used to transfer data to the server, used to update or replace the entire content of the specified resource. The PUT request requires the client to provide complete resource content, that is, to completely replace the old resource content with new data.

  4. DELETE : Used to request the server to delete the specified resource.

  5. HEAD : Similar to a GET request, but does not return a response body, only response headers, for checking the metadata and status of the resource.

  6. OPTIONS : Used to obtain the HTTP request method supported by the server and the supported header information, which is used for preflight requests of cross-domain requests.

  7. PATCH : Used for partial updates to resources, the request contains the modifications to be applied to the resources.

  8. CONNECT : Used for tunneling between proxy server requests and target servers, usually for HTTPS requests.

  9. TRACE : Used to echo the requests received by the server, mainly for debugging and diagnosis.

The above are the common request methods in the HTTP protocol. Each method has its specific semantics and uses. Developers can choose the appropriate request method according to specific business requirements and operation types.

5. The difference between HTTP 1.1 and HTTP 2.0

HTTP/1.1 and HTTP/2.0 are two different versions of the HTTP protocol. They have the following differences in performance, multiplexing, header compression, request priority, etc.:

  1. Performance :

    • HTTP/1.1: Use serialization to send requests, that is, one request and one response, and there is a single thread between the request and the response. If a request needs to wait, subsequent requests will also be blocked, resulting in slow page loading.
    • HTTP/2.0: Introduced binary transmission, multiple requests and responses can be multiplexed on one connection at the same time, avoiding blocking and improving performance.
  2. Multiplexing :

    • HTTP/1.1: Each request needs to establish and maintain a separate TCP connection, and a connection can only process one request at a time, resulting in low performance.
    • HTTP/2.0: Introduces the feature of multiplexing, which can process multiple requests and responses on a TCP connection at the same time, avoiding connection establishment and closing overhead, and improving concurrency performance.
  3. Head Compression :

    • HTTP/1.1: Each request and response contains a large amount of header information, which will increase the size of the data transfer if not compressed.
    • HTTP/2.0: Use the HPACK compression algorithm to compress header information, reducing the size of data transmission and improving performance.
  4. Request priority :

    • HTTP/1.1: All requests are equal, and the server cannot prioritize important requests.
    • HTTP/2.0: Introduced the concept of request priority, different priorities can be set for requests, and the server can prioritize important requests according to the priority.
  5. Server push :

    • HTTP/1.1: The server can only return a response after receiving a client request.
    • HTTP/2.0: Introduced the feature of server push, the server can actively push resources related to client requests, reducing the number of client requests and improving performance.

In general, HTTP/2.0 has a greater performance improvement than HTTP/1.1, mainly reflected in multiplexing, header compression, request priority, and server push. Therefore, for servers and clients that support HTTP/2.0, it is recommended to use HTTP/2.0 as much as possible to improve website performance and loading speed.

6. The difference between HTTP and HTTPS protocols

HTTP (Hypertext Transfer Protocol) and HTTPS (Hypertext Transfer Protocol Secure) are two different protocols, and they have the following differences in terms of security and data transmission:

  1. Security :

    • HTTP: It is a clear text protocol, and the data is not encrypted during transmission, which is easy to be eavesdropped and tampered with.
    • HTTPS: It is an encryption protocol, using SSL/TLS to encrypt data transmission, which can ensure the security of data during transmission and prevent data from being eavesdropped and tampered with.
  2. Encryption method :

    • HTTP: No encryption, data is transmitted in clear text.
    • HTTPS: use SSL/TLS protocol for encryption, and use public key and private key to ensure the security of data transmission.
  3. port :

    • HTTP: Port 80 is used by default.
    • HTTPS: Port 443 is used by default.
  4. Certificate :

    • HTTP: No certificate required.
    • HTTPS: A server-side SSL certificate is required to verify the identity of the server and encrypt communication.
  5. speed :

    • HTTP: Transfer speeds are generally faster because there is no need to encrypt and decrypt data.
    • HTTPS: Since the data needs to be encrypted and decrypted, the transmission speed is usually slower than HTTP.
  6. Usage scenario :

    • HTTP: Applicable to scenarios that do not involve the transmission of sensitive information, such as ordinary web page browsing, static resource downloading, etc.
    • HTTPS: Applicable to scenarios involving sensitive information transmission, such as login, payment, and personal information submission.

Generally speaking, HTTPS is more secure and reliable than HTTP protocol, and is suitable for scenarios where user privacy and sensitive information need to be protected. Most websites now use the HTTPS protocol to ensure the secure transmission of data, especially for websites involving user information and payment information, using HTTPS is a very important security measure.

7. What is the header compression algorithm of HTTP2?

The header compression algorithm used by HTTP/2 is HPACK (Header Compression for HTTP/2). HPACK compresses HTTP header information by using dictionary encoding and differential encoding, thereby reducing the size of data transmission and improving performance.

The steps for HPACK to compress header information are as follows:

  1. Static table : HPACK defines a static table that contains a set of predefined header fields and values ​​that are very common in HTTP requests and responses. Static tables do not need to be repeatedly transmitted during compression, and only need to send a static table index, and the receiver can search for the corresponding fields and values ​​locally.

  2. Dynamic table : HPACK also uses a dynamic table to store previously transmitted header fields and values, which can be reused in subsequent requests and responses, avoiding repeated transmissions.

  3. Dictionary encoding : During the compression process, HPACK compares the header fields and values ​​with the static and dynamic tables. If a matching field and value can be found, the index can be used to replace the original field and value, thereby implementing dictionary encoding.

  4. Differential encoding : If no matching fields and values ​​can be found in the static and dynamic tables, HPACK will use differential encoding to represent the difference between the fields and values ​​and the previously transmitted header. Differential encoding can further reduce the amount of data transmission, especially when there are many similar header information in the request and response.

Through static tables, dynamic tables, dictionary encoding, and differential encoding, HPACK can efficiently compress HTTP header information, reduce the size of data transmission, and improve the performance of HTTP/2. Header compression is an important feature of the HTTP/2 protocol, which has a significant impact on network transmission and page loading performance.

8. Talk about HTTP 3.0

HTTP/3.0, also known as QUIC (Quick UDP Internet Connections), is the next-generation version of the HTTP protocol, launched by Google. It is built on the UDP protocol, which is very different from the previous HTTP versions (HTTP/1.1 and HTTP/2) which are based on the TCP protocol. The main features and improvements of HTTP/3 include:

  1. UDP-based protocol : HTTP/3 no longer uses TCP as the transport layer protocol, but uses UDP. The purpose of this is to solve the congestion control and head-of-line blocking problems of TCP, and improve the performance and stability of the connection.

  2. Multiplexing : HTTP/3 inherits the multiplexing feature of HTTP/2, and can process multiple requests and responses simultaneously on one connection, avoiding connection establishment and closing overhead, and improving concurrency performance.

  3. 0-RTT connection : HTTP/3 introduces 0-RTT connection, that is, data can be sent when the connection is established, without waiting for the completion of the handshake. This speeds up connection establishment and data transfer.

  4. Fast recovery : HTTP/3 adopts a fast recovery mechanism. When a data packet in a connection is lost or an error occurs, the connection can be restored faster, reducing the time for connection interruption.

  5. Connection migration : HTTP/3 supports connection migration, that is, when the client switches networks or the IP address changes, data transmission can continue without interrupting the connection.

  6. Header compression : HTTP/3 uses the HPACK header compression algorithm similar to HTTP/2 to reduce the transmission size of header information.

HTTP/3 provides faster connection establishment speed, more stable connection performance and more efficient data transmission by using UDP protocol and optimized transmission mechanism, further improving the performance and user experience of web applications. At present, HTTP/3 is still in the experimental stage, but it has been supported and tested in some mainstream browsers and servers, and it is expected to become the mainstream version of the HTTP protocol in the future.

9. What is the HTTPS protocol?

HTTPS (Hypertext Transfer Protocol Secure) is a secure version of the HTTP protocol. It adds SSL/TLS encryption technology on the basis of HTTP to protect the security and integrity of data during transmission. By using HTTPS, you can ensure that sensitive user information (such as user name, password, credit card number, etc.) is encrypted when transmitted between the client and the server, preventing it from being stolen and tampered with by a third party.

HTTPS works as follows:

  1. Handshake phase : When the client initiates an HTTPS request, it first shakes hands with the server, negotiates the encryption protocol, and determines the encryption algorithm and key used.

  2. Certificate verification : The server will send a digital certificate to the client, which contains the server's public key and the signature of the certificate. The client will use the preset root certificate or certificate chain to verify whether the server's certificate is legal and trustworthy.

  3. Key exchange : After certificate verification, the client generates a temporary symmetric key, encrypts it with the server's public key, and sends it to the server. The server uses its own private key to decrypt and obtain the symmetric key.

  4. Encrypted communication : The client and server then use symmetric keys to encrypt and decrypt data to ensure the security and integrity of data during transmission.

The advantages of HTTPS are mainly reflected in the following aspects:

  1. Data encryption : HTTPS uses SSL/TLS encryption technology to ensure that data is encrypted during transmission to prevent data from being stolen and tampered with.

  2. Authentication : HTTPS uses digital certificates to verify the identity of the server, ensuring that users connect to the correct server and preventing man-in-the-middle attacks.

  3. SEO optimization : Search engines are more likely to include websites that use HTTPS because it provides a more secure user experience.

  4. User trust : HTTPS increases user trust in the website by displaying a green security lock icon and HTTPS logo.

  5. Compliance with regulations : Several regulations and standards require websites to use HTTPS to protect user privacy and sensitive information.

Generally speaking, HTTPS is a secure protocol that can protect the security and integrity of data during transmission. For websites and applications involving user privacy and sensitive information, using HTTPS is a very important security measure.

10. HTTPS communication (handshake) process

The handshake process of HTTPS communication is also called TLS handshake (Transport Layer Security Handshake), which is a key step in establishing an HTTPS connection to ensure secure communication between the client and the server. The following are the brief steps of the HTTPS communication handshake process:

  1. The client sends a client Hello message : the client sends a client Hello message to the server, which includes supported encryption algorithms, protocol version numbers, and other information.

  2. The server replies with a server Hello message : After receiving the client Hello message, the server returns a server Hello message, which includes the confirmed encryption algorithm, protocol version number and other information.

  3. The server sends a digital certificate : The server sends its own digital certificate to the client, which contains the server's public key and the signature of the certificate.

  4. Client verification certificate : The client uses the preset root certificate or certificate chain to verify whether the server's certificate is legal and trustworthy.

  5. The client generates a temporary key : the client generates a temporary symmetric key, encrypts it with the server's public key, and sends it to the server.

  6. The server decrypts the temporary key : the server uses its own private key to decrypt the temporary key sent by the client to obtain the symmetric key.

  7. Handshake completed : At this point, the handshake process is completed, both the client and the server have the same symmetric key, and subsequent communications will use the symmetric key to encrypt and decrypt data.

The entire handshake process is performed on an encrypted channel to prevent man-in-the-middle attacks and eavesdropping. Through the handshake process, the client and server negotiate a set of secure encryption algorithms and keys to ensure the security and integrity of the HTTPS connection. Once the handshake is complete, subsequent HTTP requests and responses will use this secure encrypted channel for data transmission.

おすすめ

転載: blog.csdn.net/weixin_52003205/article/details/131842028