我们可以创建自定义 HTTP 状态代码吗?

我有一个 REST 和 WCF 服务,并希望根据操作发送自定义状态代码。

例如,当某些验证失败时,我想发送 HTTP444,当授权失败时,我想发送 HTTP455

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

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

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

128695 次浏览

不,您只能使用 rfc 文档需求代码,详情请参阅 RFC1945

我建议不要在 applicable codes already exist中为您想要在示例中执行的操作创建自己的 HTTP 状态代码。

来自 https://www.rfc-editor.org/rfc/rfc4918#section-11.2:

422[ Unprocessable Entity ]状态代码意味着服务器理解请求实体的内容类型(因此415[ Unsupport Media Type ]状态代码是不合适的) ,请求实体的语法是正确的(因此400[ Bad Request ]状态代码是不合适的) ,但无法处理所包含的指令。例如,如果 XML 请求体包含格式良好(即语法正确)但语义错误的 XML 指令,则可能发生此错误条件。

可以说,“无法处理”可能是由于验证错误。

是的,只要你尊重这个类——也就是说,2xx 代表成功,4xx 代表客户端错误,等等。因此,您可以为您自己的应用程序的错误条件返回自定义的4XX 错误代码(最好是那些未分配的错误代码)。

引自[ RFC 2616][1] :

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

Class'

  • 1xx: Informational - Request received, continuing process

  • 2xx: Success - The action was successfully received, 理解并接受

  • 3xx: 重定向-必须采取进一步的行动,以便 完成请求

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

  • 5xx: 服务器错误-服务器未能实现一个明显的 有效要求[1] :

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

是的,您可以添加自定义错误代码。如果可能的话,请使用已经存在的代码,如果要声明新的代码,请小心避免冲突。

您应该知道,有些代理会过滤未知代码 。我与用户有问题,在后面的代理,映射5XX 到500,和4XX 到404。这使得我的 ajax 调用在检查状态代码时失败。

有些应用程序在600-799范围内添加自定义响应代码

基调定义的错误代码(600-799)

600: CONNECTION ERROR - This indicates a general connection error
601: INCOMPLETE ERROR - This indicates sever sends an incomplete page/object (as indicated by Content-Length header)
602: UNEXPECTED CLOSE ERROR - This indicates socket connection has been closed unexpectedly
603: REFUSED ERROR - This indicates a request to connect to the server is refused
604: TIMEOUT ERROR - This indicates there is no activity in socket connection in 3 minutes
605: REDIRECT ERROR - This indicates an error in redirect HTTP header
606: SSL ERROR - This indicates a general error in SSL
607: HEADER ERROR - This indicates a malformed HTTP header
608: EMPTY RESPONSE ERROR - This indicates server doesn't send any response after a request is sent
609: UNKNOWN HOST ERROR - This indicates socket receives an unknown host error from DNS
610: NO ROUTE TO HOST ERROR - This indicates a no route to host error was received while attempting to open a socket
611: SOCKET ERROR - This indicates a general socket error
612: FRAME LOOP ERROR - This indicates a page has a frame loop (frame A includes Frame B that includes Frame A)
613: REDIRECT LOOP ERROR - This indicates a page has a redirect loop (page A redirects to page B that redirects to page A)
614: CONNECTION RESET ERROR - This indicates socket receive a reset signal from the server
615: SOCKET PROTOCOL ERROR - This indicates an error in socket protocol
616: SOCKET BIND ERROR - This indicates an error in binding the socket
617: CONNECTION ERROR - This indicates a general socket connection error
618: CHUNK ERROR - This indicates an error in chunked encoding
619: SSL TIMEOUT - This indicates a timeout during SSL handshake (2 minutes)
620: SSL END OF INPUT - This indicates an end-of-file is received during SSL handshake
621: SSL HANDSHAKE ERROR - This indicates a general error during SSL handshake
622: SSL CERTIFICATE ERROR - This indicates an error in SSL certificate verification
623: SSL AUTHENTICATION ERROR - This indicates an authentication error during SSL handshake
624: SSL BAD MAC ERROR - This indicates a bad MAC during SSL handshake
625: SSL CIPHER ERROR - This indicates a cipher error during SSL handshake
701: ERROR TEXT FOUND - This code is returned if any error text (such as, "Service Unavailable") are found in the main page (frame HTML contents included). Note that the error text must be defined in advance of the test. Error text means if the text is found, this session should be considered a failure.
702: REQUIRED TEXT NOT FOUND - This code is returned If not all required texts are found in the main page. Note that required text must be defined in advance of the test. Required text means if the text is not found, this session should be considered a failure.
703: HTML BODY EMPTY - This code is returned if the HTML body of the page is empty (only if error text or required text has been defined).

这是否是一个好的做法,我不敢说,但它至少是一个有趣的参考。

下面是所有可用/不可用 HTTP代码的完整列表。

https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml

例如,在 4XX系列中可以使用下面的箭头(因为它们是未分配的)。

enter image description here