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

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

当一些validation失败的例子,然后我想发送HTTP 444,当授权失败,我想发送HTTP 455

问题是我们如何validationSOAP和REST Web服务。

在客户端上,错误代码如何起作用,因为当您从WCF服务(使用SOAP)发送HTTP 400/500时,将在客户端显示状态码。

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

是的,只要你尊重class级,即2xx成功,4xx客户端错误等,所以你可以返回自定义的4XX错误代码(最好是那些未分配的)为您自己的应用程序的错误条件。

引用[RFC 2616] [1]:

“HTTP状态代码是可扩展的,HTTP应用程序不需要了解所有已注册的状态代码的含义,尽pipe这种理解显然是可取的,但是应用程序必须理解任何状态代码的类,如第一位所示,任何无法识别的响应等同于该类的x00状态码,除了不能识别的响应不能被caching,例如,如果客户端收到一个无法识别的状态码431,则可以安全地假定它的请求有问题,并且把它看作是收到400状态码。

类'

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

  • 2xx:成功 – 行动成功接受,理解和接受

  • 3xx:redirect – 必须采取进一步行动才能完成请求

  • 4xx:客户端错误 – 请求包含错误的语法或无法完成

  • 5xx:服务器错误 – 服务器无法完成一个明显有效的请求[1]:

http://tools.ietf.org/html/rfc2616#section-6.1.1

我build议不要创build自己的HTTP状态代码,只要适用的代码已经存在 ,就可以在您的示例中进行操作。

  • validation失败:状态422
  • 授权失败:状态403

是的,您可以添加自定义错误代码。 如果可能的话,使用已经存在的代码,如果你宣布新的代码,小心避免碰撞。

你应该知道,虽然有些代理过滤未知的代码 。 我遇到的问题是用户在代理后面将5XX映射到500,将4XX映射到404。这使得我的ajax调用在检查状态代码失败的地方。

某些应用程序在600-799范围内添加自定义响应代码。 例如在这里检查KeyNote中的响应代码列表

Keynote定义的错误代码(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). 

不pipe这是不是好的做法,我都不敢说,至less这是一个有趣的参考。

不,您只能使用rfc文档要求代码,详见RFC1945