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

时间:2021-07-20 20:50:06

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

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

Example when some validation fails then i want to send HTTP 444 and when authorization fails i want to send HTTP 455

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

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

问题是如何对SOAP和REST web服务进行验证。

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

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

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

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

5 个解决方案

#1


76  

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.

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

To quote from [RFC 2616][1]:

引用[RFC 2616][1]:

"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."

HTTP状态码是可扩展的。HTTP应用程序不需要理解所有已注册状态代码的含义,尽管这种理解显然是可取的。但是,应用程序必须理解任何状态码的类(如第一个数字所示),并将任何未识别的响应视为与该类的x00状态码相等,但不能缓存未识别的响应。例如,如果客户端接收到一个未被识别的状态码为431,它可以安全地假设它的请求有问题,并将响应视为收到了400个状态码。

Class'

类的

  • 1xx: Informational - Request received, continuing process

    1xx:信息-请求接收,持续过程。

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

    2xx:成功-行为被成功地接受、理解和接受

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

    3xx:重定向——为了完成请求,必须采取进一步的行动

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

    4xx:客户端错误——请求包含错误的语法或无法实现

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

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

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

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

#2


24  

I recommend against creating your own HTTP status codes, when applicable codes already exist for the things that you want to do in your example.

我建议您不要创建自己的HTTP状态代码,因为在您的示例中,已经存在适用的代码。

  • Validation failure: Status 422
  • 验证失败:地位422年
  • Authorization failure: Status 403
  • 403年授权失败:状态

#3


15  

Yes you can add custom error codes. If possible use codes that already exist though, and if you are declaring new ones be careful to avoid collisions.

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

You should be aware though that some proxies filter unknown codes. I had issues with users that where behind proxies that mapped 5XX to 500, and 4XX to 404. This made my ajax calls that where checking the status code to fail.

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

#4


4  

Some applications add their custom response codes in the range 600-799. Check for example the response code list from KeyNote here

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

Keynote Defined Error Codes (600-799)

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).

Whether this is good practice I would not dare to say, but it is an interesting reference at least.

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

#5


-11  

No, you can only use rfc documentation requirements code, see details in RFC1945

不,您只能使用rfc文档需求代码,请参阅RFC1945中的详细信息

#1


76  

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.

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

To quote from [RFC 2616][1]:

引用[RFC 2616][1]:

"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."

HTTP状态码是可扩展的。HTTP应用程序不需要理解所有已注册状态代码的含义,尽管这种理解显然是可取的。但是,应用程序必须理解任何状态码的类(如第一个数字所示),并将任何未识别的响应视为与该类的x00状态码相等,但不能缓存未识别的响应。例如,如果客户端接收到一个未被识别的状态码为431,它可以安全地假设它的请求有问题,并将响应视为收到了400个状态码。

Class'

类的

  • 1xx: Informational - Request received, continuing process

    1xx:信息-请求接收,持续过程。

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

    2xx:成功-行为被成功地接受、理解和接受

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

    3xx:重定向——为了完成请求,必须采取进一步的行动

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

    4xx:客户端错误——请求包含错误的语法或无法实现

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

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

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

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

#2


24  

I recommend against creating your own HTTP status codes, when applicable codes already exist for the things that you want to do in your example.

我建议您不要创建自己的HTTP状态代码,因为在您的示例中,已经存在适用的代码。

  • Validation failure: Status 422
  • 验证失败:地位422年
  • Authorization failure: Status 403
  • 403年授权失败:状态

#3


15  

Yes you can add custom error codes. If possible use codes that already exist though, and if you are declaring new ones be careful to avoid collisions.

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

You should be aware though that some proxies filter unknown codes. I had issues with users that where behind proxies that mapped 5XX to 500, and 4XX to 404. This made my ajax calls that where checking the status code to fail.

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

#4


4  

Some applications add their custom response codes in the range 600-799. Check for example the response code list from KeyNote here

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

Keynote Defined Error Codes (600-799)

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).

Whether this is good practice I would not dare to say, but it is an interesting reference at least.

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

#5


-11  

No, you can only use rfc documentation requirements code, see details in RFC1945

不,您只能使用rfc文档需求代码,请参阅RFC1945中的详细信息