程序员应该关注哪些 HTTP 状态代码?
因此,如果您查看 HTTP 状态代码列表,就会发现可能其中一些在编程时会很有用。服务器可能会处理一些事情,例如协议,但是其中许多代码对于告诉浏览器页面的实际状态可能很有用。
那么,我的问题是我们应该关注这些状态代码中的哪一个?我们应该检查哪些内容来发送,哪些内容很可能永远不会在常规应用程序编程中使用。
如果您好奇,这属于 PHP 编程的范围,但它可能也适用于其他语言。
So, if you look at the List of HTTP Status Codes, there are probably a number of them that would be useful while programming. The server might handle some things, like protocols, but a lot of these codes could be useful in telling the browser the actual status of the page.
So, my question is which of these status codes should we be concerned with? Which should we be checking to send, and which ones will most likely never be used in regular application programming.
If you are curious, this is in the scope of PHP programming, but it would probably apply to other languages just as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
其中许多对于 REST 风格的 API 使用来说本质上是有用的。例如:
200(确定):您请求资源。就是这样!
201(已创建):您要求我创建新资源。我做到了!下次您可以到这里向我索取。
202(已接受):您要求我做某事,但这需要一段时间,所以不要等待。您可以在这里查看状态。
300(多项选择):您提出了一些要求,但不够具体。您指的是哪一项?
301(永久移动):您请求了某些东西,但它现在在其他地方。它去了这里。
302(已找到):您请求了某件事,但目前它在其他地方。在这里。
304(未修改):您在此之前请求过某些内容,但自上次您请求我以来它没有改变。
400(错误请求):您要求我做的事情有问题。修改您所说的内容,然后重试。
401(未经授权):我需要您先表明身份,然后我才能完成此请求。 [注意:这是命名最不幸的标头之一。它确实应该被命名为未验证; 403 更像是未经授权。]
403(禁止):您请求了您不被允许拥有的东西。
404(未找到):您请求了资源,但没有与您的描述相符的资源。
500(服务器错误):出了点问题,所以我现在无法为您提供您所要求的内容。抱歉。
501(未实施):我现在不支持此类请求。
503(服务不可用):我现在无法响应请求。
Many of these are intrinsically useful with REST-style API usage. For example:
200 (OK): You asked for a resource. Here it is!
201 (Created): You asked me to make a new resource. I did! Here's where you can go to ask me for it next time.
202 (Accepted): You asked me to do something, but it's going to take a while, so don't wait up. Here's where you can go to check up on the status.
300 (Multiple Choices): You asked for something, but you weren't specific enough. Which one of these did you mean?
301 (Moved Permanently): You asked for something, but it's somewhere else now. Here's where it went.
302 (Found): You asked for something, but it's somewhere else for the moment. Here it is.
304 (Not Modified): You asked for something before this, but it hasn't changed since the last time you asked me.
400 (Bad Request): Something is wrong about what you asked me to do. Fix what you said and try again.
401 (Unauthorized): I need you to identify yourself before I can finish this request. [Note: This is one of the more unfortunately named headers. It should really be titled Unauthenticated; 403 is more like Unauthorized.]
403 (Forbidden): You asked for something you're not allowed to have.
404 (Not Found): You asked for a resource, but there isn't one that matches your description.
500 (Server Error): Something went wrong, so I can't give you what you asked for right now. Sorry about that.
501 (Not Implemented): I don't support that kind of request right now.
503 (Service Unavailable): I'm not able to respond to requests right now.
更准确地说,这些只是 HTTP 状态代码,而不是 HTTP 标头。标头传达了很多内容,由客户端和服务器发送,超出了本答案的范围。
其中一个 HTTP 标头,即服务器发送到客户端的第一个标头,如下所示:
或:
协议标识符
HTTP/1.x
之后出现的数字称为状态代码并在其后发送相应的状态消息。以下是我在 PHP 编程期间必须使用的状态代码:200 OK
是迄今为止最常见的。这意味着一切都运行良好并且您正在以内容进行响应。404 未找到
在某些条件下,特别是当请求导致在服务器上找不到正在执行的脚本时,服务器会自动发送。有时,特别是如果您正在编写以特殊方式处理 URI 的框架,您将需要手动设置 404 状态代码。例如,如果您有一个中央执行脚本index.php
,同时您使用 .htaccess 或 Apache 设置路由所有请求,则 Apache 几乎永远不会自行返回 404,因为毕竟,它已找到index.php
。但显然,仍然有一些您想要通信的 URI 不会通向任何地方,您需要为其发送自己的 404 状态标头。301 永久移动
和找到302
(更常被称为“临时移动”)。这两个命令指示浏览器查找Location
标头并将用户重定向到其中指定的 URL。大多数 PHP 框架都有自己的 HTTP 重定向函数,这些函数也处理标头。原生 PHP 重定向header('Location: http://www.google.com');
自动将 HTTP 状态更改为 302。我从来没有真正深入理解 302 和 301 之间的区别,但我读到 301 对于搜索引擎优化要好得多,所以我尝试始终使用 301。也许其他人可以启发确切的区别是什么。需要注意的一件事是避免将 301/302 状态和位置标头放置在旨在接收 POST 数据的页面上。我过去遇到过一些麻烦。304 未修改
通常根据您的 Apache 设置自动发送。正常情况下,大多数浏览器都包含所请求项目在用户计算机上缓存的日期/时间。 ETag 和其他标头用于此目的。如果Apache判断服务器对应的文件从那时起就没有改变,Apache经常会发送一个没有内容的304,这只是告诉客户端使用缓存的版本。401 未经授权
是当用户尝试访问网站上的受限部分时发送。有一些旧的 HTML 功能和服务器技术支持本机用户名/密码提示,当提示被取消或未授权时,会发送 401 状态代码。如今,大多数人都编写自己的 PHP 实现来进行用户身份验证和权限管理,因此 Apache 并不经常自行发送 401。您可以手动发送状态以指示需要更多权限才能访问该页面。400 错误请求
如果 Apache 收到它无法理解的请求,则由 Apache 发送。您通常不必担心手动发送。403 禁止
是当用户尝试访问他们无法访问的区域时,某些人会使用该方法,即使由于地理、IP 或禁止限制而进行了正确的身份验证。我自己不用,只是用401和404来填写。5xx
。 500 系列是您作为开发人员实际上不会看到的代码。这意味着您的代码或服务器做了坏事。如果您有足够能力的服务器或负载平衡系统,并且代码中没有错误,您将永远不会看到 500 系列。To be more precise, these are just HTTP status codes, not HTTP headers. Headers convey a lot of things and are sent by both the client and the server, and are beyond the scope of this answer.
One of the HTTP headers, namely the first one sent by the server to the client, looks like this:
or:
The number that appears after the protocol identifier
HTTP/1.x
is what's called the status code with the corresponding status message sent after it. Here are the status codes that I've had to use in my PHP programming days:200 OK
is by far the most common. It means that everything has worked fine and that you're responding with content.404 Not Found
is automatically sent by the server under certain conditions, in particular when the request leads to an executing script that cannot be found on the server. Sometimes, especially if you're writing frameworks which handle URIs in a special way, you will want to manually set a 404 status code. For example, if you have one central executing scriptindex.php
through while you route all requests using .htaccess or your Apache settings, Apache will almost never return a 404 on its own accord because, after all, it has foundindex.php
. But clearly, there will still be some URIs that you want to communicate don't lead to anywhere, for which you'll want to send your own 404 status header.301 Moved Permanently
and302 Found
(more commonly referenced as 'Moved Temporarily'). These two instruct the browser to look for aLocation
header and to redirect the user to the URL specified there. Most PHP frameworks have their own functions for HTTP redirects, which also handle the headers. The native PHP redirectheader('Location: http://www.google.com');
automatically changes the HTTP status to 302. I've never really understood in depth the difference between 302 and 301, but I've read that 301 is much better for Search Engine Optimization, so I try to always use 301. Perhaps someone else can enlighten on what the exact difference is. One thing to be careful of is to avoid putting a 301/302 status and Location header on a page that's intended to receive POST data. I've had some trouble with it in the past.304 Not Modified
is usually sent automatically depending on your Apache settings. Most browsers under normal conditions include the date/time on which the requested item was cached on the user's computer. ETags and other headers are used for this purpose. If Apache judges that the server's corresponding file has not changed since that time, Apache will often send a 304 with no content, which just tells the client to use the cached version.401 Unauthorized
is sent when a user is trying to access a restricted section on the website. There are some old HTML features and server technologies that support native username/password prompts, which sent 401 status codes when the prompts were cancelled or not authorized. Most people these days write their own PHP implementations for user authentication and rights management, so Apache doesn't often send 401s on its own accord. You can send the status manually to indicate that more rights are needed to access the page.400 Bad Request
is sent by Apache if it receives a request it can't understand. You usually don't have to worry about sending it manually.403 Forbidden
is used by some people when users are trying to access a area that they would not be able to access, even with proper authentication perhaps due to geographic, IP, or banning restrictions. I don't use it myself, and I just use 401 and 404 to fill in.5xx
. The 500-series are the codes you really don't to see as a developer. It means your code or server did something bad. If you have a server or a load-balancing system of sufficient calibre and you don't have errors in your code, you'll never see the 500-series.好吧,这些是状态代码,而不是标头,但它们中的任何一个都可能有用(尽管 5xx 系列不太可能有用)。
Well, those are status codes, not headers, but any of them might be useful (although the 5xx series are unlikely to be).
我认为您谈论的是使用标头来提供文件或提供 RESTful Web 服务?
那么您需要的是状态代码,而不是标头。我常用的有:
当然,对于 RESTful Web 服务,您可以更改文本以使其更具描述性,并在正文中提供描述。
然后是:
I take it your talking about using headers for either serving files or providing a RESTful webservice?
You'd be after status codes, rather than headers then. The ones I've commonly used are:
Of course, for RESTful webservices you can change the text to be more descriptive as well as providing description in the body.
Then there's:
快速浏览一下该列表(状态代码),以下是我经常使用的列表(我的工作是 PHP Web 开发):
200 OK
:几乎总是由 Apache 发送301 永久移动
:通常由我发送(或由 Apache,当使用重写规则时)302 Found
:通常由我发送(或由 Apache,当使用重写规则时)304 Not Modified
:通常由 Apache(或其前面的反向代理)发送401 Unauthorized
:通常由 Apache 发送403 Forbidden
:通常由 Apache404 未找到
< 发送/a> :由 Apache 和我发送 - 取决于情况410 Gone
:通常由我发送500 内部服务器错误
:由 Apache 和我发送;这是我在出现“技术”错误时通常使用的方法 - 即使它永远不会发生 ^^以下是我可以使用的方法(尤其是在进行 REST 时):
201 已创建
400 错误请求
需要 402 付款
不允许使用 405 方法
501 未实现
503 服务不可用
Quickly going through that list (of status codes), here are those I often use (I am doing PHP web-development as my job) :
200 OK
: almost always sent by Apache301 Moved Permanently
: generally sent by me (or by Apache, when using rewrite-rules)302 Found
: generally sent by me (or by Apache, when using rewrite-rules)304 Not Modified
: generally sent by Apache (or a reverse proxy in front of it)401 Unauthorized
: generally sent by Apache403 Forbidden
: generally sent by Apache404 Not Found
: sent both by Apache and me -- depending on the situations410 Gone
: generally sent by me500 Internal Server Error
: sent both by Apache and me ; that's the one I generally use when there's a "technical" error -- even if it should never happen ^^And here are those I could use (especially if doing REST) :
201 Created
400 Bad Request
402 Payment Required
405 Method Not Allowed
501 Not Implemented
503 Service Unavailable
我最常用的是:
The ones I've used most are: