Parsing and analysis of different types of HTTP status codes: common 2xx, 3xx, 4xx and 5xx status codes

PHPz
Release: 2023-12-26 17:36:59
Original
1340 people have browsed it

Parsing and analysis of different types of HTTP status codes: common 2xx, 3xx, 4xx and 5xx status codes

Interpretation of HTTP protocol status codes: analysis of common 2xx, 3xx, 4xx and 5xx status codes, specific code examples are required

HTTP protocol is a protocol used for network transmission An application layer protocol that defines the rules for communication between clients and servers. During the process of HTTP request and response, the server will return a status code to indicate the processing result of the request. Status codes are divided into five categories, namely 1xx, 2xx, 3xx, 4xx and 5xx. This article will focus on the common 2xx, 3xx, 4xx and 5xx status codes and provide specific code examples.

  1. 2xx status code: The
    2xx status code indicates that the request was successfully processed. The most common status code is 200, which means that the server successfully processed the client's request. The following are several common 2xx status codes, their meanings and sample codes:

200 OK: Indicates that the request is successful and the requested resource is returned. For example, a 200 status code is returned when retrieving the content of a web page.

204 No Content: Indicates that the request was successful, but the server did not return any content. For example, when performing a delete operation on a resource, the server can return a 204 status code.

206 Partial Content: Indicates that the server successfully processed part of the request and returned partial content. For example, when requesting a range of files through the HTTP Range header field, the server may return a 206 status code.

The following is a sample code that returns a 206 status code:

HTTP/1.1 206 Partial Content
Content-Length: 10000
Content-Range: bytes 0-9999/20000
Content-Type: video/mp4

[视频的字节范围]
Copy after login
  1. 3xx status code:
    3xx status code indicates redirection. When the server receives the client's request, it will redirect the client to another location. The following are several common 3xx status codes, their meanings and sample codes:

301 Moved Permanently: Indicates that the requested resource has been permanently moved to a new location. For example, a web page is permanently redirected to another URL.

302 Found: Indicates that the requested resource has been temporarily moved to a new location. For example, a webpage temporarily redirects to another URL.

307 Temporary Redirect: Indicates that the requested resource has been temporarily moved to a new location, and the client should continue to use the original URL to send requests. The difference with the 302 status code is that the client should keep the original request's HTTP method unchanged and resend it.

The following is a sample code that returns a 301 status code:

HTTP/1.1 301 Moved Permanently
Location: https://www.new-url.com/
Copy after login
  1. 4xx status code:
    4xx status code indicates a client error. When the server cannot handle the request sent by the client, it returns a 4xx status code. The following are several common 4xx status codes, their meanings and sample codes:

400 Bad Request: Indicates that the request sent by the client has a syntax error. For example, a required parameter is missing from the request.

401 Unauthorized: Indicates that the request requires authentication, but the client did not provide valid credentials. For example, a 401 status code is returned when trying to access a page that requires login.

404 Not Found: Indicates that the requested resource was not found. For example, the client requested a URL that does not exist.

The following is a sample code that returns a 404 status code:

HTTP/1.1 404 Not Found
Content-Type: text/html

<!DOCTYPE html>
<html>
<head>
<title>404 Not Found</title>
</head>
<body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
</body>
</html>
Copy after login
  1. 5xx status code:
    5xx status code indicates a server error. When the server cannot complete the request, it returns a 5xx status code. The following are several common 5xx status codes, their meanings and sample codes:

500 Internal Server Error: Indicates that an internal error occurred while the server was processing the request. For example, server configuration errors or code logic errors.

503 Service Unavailable: Indicates that the server is temporarily unable to process the request. This is usually caused by server overload or maintenance. For example, when the website is undergoing an upgrade, a 503 status code can be returned.

The following is a sample code that returns a 500 status code:

HTTP/1.1 500 Internal Server Error
Content-Type: text/html

<!DOCTYPE html>
<html>
<head>
<title>500 Internal Server Error</title>
</head>
<body>
<h1>Internal Server Error</h1>
<p>An unexpected condition was encountered while processing the request.</p>
</body>
</html>
Copy after login

By understanding the meaning of common 2xx, 3xx, 4xx and 5xx status codes and mastering the corresponding code examples, you can better To cope with various situations that may be encountered during HTTP requests. This enables more efficient development and debugging, and improves user experience.

The above is the detailed content of Parsing and analysis of different types of HTTP status codes: common 2xx, 3xx, 4xx and 5xx status codes. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template