我们可以创建自定义 HTTP 状态代码吗? [英] Can we create custom HTTP Status codes?

查看:46
本文介绍了我们可以创建自定义 HTTP 状态代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 REST 和 WCF 服务,想根据操作发送自定义状态代码.

I have a REST and WCF service and want to send a custom status code based on the operation.

例如,当某些验证失败时,我想发送 HTTP 444,当授权失败时,我想发送 HTTP 455

For example when some validation fails then I want to send HTTP 444 and when authorization fails I want to send HTTP 455

问题是我们如何为 SOAP 和 REST Web 服务验证它.

The question is how do we have it validated for both SOAP and REST web services.

在客户端上,错误代码如何起作用,因为当您从 WCF 服务(使用 SOAP)发送 HTTP 400/500 时,客户端会抛出一个异常,显示状态代码?

On the client how does the error code act because when you send an HTTP 400/500 from a WCF Service (using SOAP) an exception is thrown on the client showing the status code?

现在,如果我发送一个新的自定义状态代码,客户端将如何处理?

Now if I send a new custom status code how does the client handle this?

推荐答案

是的,只要你尊重类——也就是说,2xx 表示成功,4xx 表示客户端错误,等等.所以你可以返回自定义的 4XX 错误代码(最好是那些未分配的)用于您自己的应用程序的错误情况.

Yes, as long as you respect the class -- that is, 2xx for success, 4xx for Client error, etc. So you can return custom 4XX error codes (preferably those that are unassigned) for your own application's error conditions.

引自 [RFC 2616][1]:

To quote from [RFC 2616][1]:

"HTTP 状态代码是可扩展的.不需要 HTTP 应用程序了解所有注册状态码的含义,尽管这样理解显然是可取的.但是,应用程序必须了解任何状态代码的类别,如第一个所示数字,并将任何无法识别的响应视为等同于该类的 x00 状态代码,除了不得缓存无法识别的响应.例如,如果一个客户端接收到无法识别的 431 状态码,可以安全地假设它的请求有问题,并且将响应视为收到 400 状态代码."

"HTTP status codes are extensible. HTTP applications are not required to understand the meaning of all registered status codes, though such understanding is obviously desirable. However, applications MUST understand the class of any status code, as indicated by the first digit, and treat any unrecognized response as being equivalent to the x00 status code of that class, with the exception that an unrecognized response MUST NOT be cached. For example, if an unrecognized status code of 431 is received by the client, it can safely assume that there was something wrong with its request and treat the response as if it had received a 400 status code."

班级

  • 1xx:信息性 - 收到请求,继续处理

  • 1xx: Informational - Request received, continuing process

2xx:成功 - 成功接收到动作,理解并接受

2xx: Success - The action was successfully received, understood, and accepted

3xx:重定向 - 必须采取进一步措施才能完成请求

3xx: Redirection - Further action must be taken in order to complete the request

4xx:客户端错误 - 请求包含错误的语法或不能满足

4xx: Client Error - The request contains bad syntax or cannot be fulfilled

5xx:服务器错误 - 服务器未能完成一个明显的有效请求 [1]:

5xx: Server Error - The server failed to fulfill an apparently valid request [1]:

https://www.rfc-editor.org/rfc/rfc2616#section-6.1.1

这篇关于我们可以创建自定义 HTTP 状态代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆