程序员应该关注哪些 HTTP 状态代码?

发布于 2024-08-17 11:18:20 字数 294 浏览 4 评论 0原文

因此,如果您查看 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

眉目亦如画i 2024-08-24 11:18:20

其中许多对于 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.

楠木可依 2024-08-24 11:18:20

更准确地说,这些只是 HTTP 状态代码,而不是 HTTP 标头。标头传达了很多内容,由客户端和服务器发送,超出了本答案的范围。

其中一个 HTTP 标头,即服务器发送到客户端的第一个标头,如下所示:

HTTP/1.x 200 OK

或:

HTTP/1.x 404 Not Found

协议标识符 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:

HTTP/1.x 200 OK

or:

HTTP/1.x 404 Not Found

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 script index.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 found index.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 and 302 Found (more commonly referenced as 'Moved Temporarily'). These two instruct the browser to look for a Location 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 redirect header('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.
说好的呢 2024-08-24 11:18:20

好吧,这些是状态代码,而不是标头,但它们中的任何一个都可能有用(尽管 5xx 系列不太可能有用)。

Well, those are status codes, not headers, but any of them might be useful (although the 5xx series are unlikely to be).

梦里兽 2024-08-24 11:18:20

我认为您谈论的是使用标头来提供文件或提供 RESTful Web 服务?

那么您需要的是状态代码,而不是标头。我常用的有:

200 OK
301 Moved Permanently
302 Found (temporary redirect)
400 Bad Request
403 Forbidden
404 Not found
500 Internal Server Error

当然,对于 RESTful Web 服务,您可以更改文本以使其更具描述性,并在正文中提供描述。

然后是:

418 I'm a teapot

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:

200 OK
301 Moved Permanently
302 Found (temporary redirect)
400 Bad Request
403 Forbidden
404 Not found
500 Internal Server Error

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:

418 I'm a teapot
独﹏钓一江月 2024-08-24 11:18:20

快速浏览一下该列表(状态代码),以下是我经常使用的列表(我的工作是 PHP Web 开发):

  • 200 OK :几乎总是由 Apache 发送
  • < a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.2" rel="nofollow noreferrer">301 永久移动 :通常由我发送(或由 Apache,当使用重写规则时)
  • 302 Found :通常由我发送(或由 Apache,当使用重写规则时)
  • 304 Not Modified :通常由 Apache(或其前面的反向代理)发送
  • 401 Unauthorized :通常由 Apache 发送
  • < a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.4" rel="nofollow noreferrer">403 Forbidden :通常由 Apache
  • 404 未找到< 发送/a> :由 Apache 和我发送 - 取决于情况
  • 410 Gone :通常由我发送
  • 500 内部服务器错误:由 Apache 和我发送;这是我在出现“技术”错误时通常使用的方法 - 即使它永远不会发生 ^^

以下是我可以使用的方法(尤其是在进行 REST 时):

Quickly going through that list (of status codes), here are those I often use (I am doing PHP web-development as my job) :

And here are those I could use (especially if doing REST) :

分開簡單 2024-08-24 11:18:20

我最常用的是:

  • 301 - 永久移动 - 如果资源永久移动到新的 url,请使用此选项。
  • 302 - 暂时移动 - 当您无法进行永久重定向时,使用此重定向。
  • 404 - 未找到。您的服务器应配置为针对无效网址提供此服务。您应该在日志中监控这些内容——太多 404 是推送失败的迹象。
  • 500 - 内部服务器错误。您的服务器应配置为在出现错误时正确发送这些信息。您应该监控日志中的 5xx 错误。

The ones I've used most are:

  • 301 - Moved Permanently - Use this if the resource is permanently moved to the new url.
  • 302 - Moved Temporarily - Use this for redirecting when you can't have a permanent redirect.
  • 404 - Not Found. Your server should be configured to serve this for invalid urls. You should monitor these in your logs--too many 404s is a sign of a bad push.
  • 500 - Internal Server Error. Your server should be configured to properly send these when there are errors. You should monitor 5xx errors in your logs.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文