java 为什么 HttpRequest.HttpMethod 是字符串而不是枚举?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6722248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why HttpRequest.HttpMethod is string instead of Enum?
提问by AhmetB - Google
In the Reference of HttpRequest.HttpMethod
of .NET Framework, request type is declared with System.String
type.
在.NET Framework的Reference 中HttpRequest.HttpMethod
,请求类型是用System.String
类型声明的。
In RFC 2616all HTTP request methods are declared (e.g. POST, GET, PUT, DELETE...).
在RFC 2616中声明了所有 HTTP 请求方法(例如 POST、GET、PUT、DELETE...)。
There's also similar behavior in HttpWebRequest
and WebRequest
classes of .NET.
在.NETHttpWebRequest
和WebRequest
类中也有类似的行为。
Java has the similar approach on HttpURLConnection#setRequestMethod(String)
method.
Java 在方法上也有类似的HttpURLConnection#setRequestMethod(String)
方法。
Why do these language designers do not consider implementing an enum for those HTTP methods?
为什么这些语言设计者不考虑为这些 HTTP 方法实现枚举?
Do you have an idea?
你有想法吗?
回答by Damien_The_Unbeliever
The first sentences of your RFC 2616link (emphasis added):
您的RFC 2616链接的第一句话(强调):
The set of commonmethods for HTTP/1.1 is defined below. Although this set can be expanded...
HTTP/1.1的常用方法集定义如下。虽然这个集合可以扩展......
That is to say, the method in HTTP may be anything. There are "well known" or common methods, the semantics of which are well understood (well, okay, shouldbe well understood - I still encounter people unclear on GET/POST).
也就是说,HTTP 中的方法可以是任何东西。有“众所周知”或通用的方法,它们的语义很好理解(好吧,好吧,应该很好理解——我仍然遇到对 GET/POST 不清楚的人)。
But any application may implement other methods. Hopefully, the semantics of those other methods will be well understood between client and server applications.
但是任何应用程序都可以实现其他方法。希望在客户端和服务器应用程序之间能够很好地理解那些其他方法的语义。
For these reasons, an enum would be inappropriate, since there can always be "other" values that wouldn't fit in that enum.
由于这些原因,枚举是不合适的,因为总有“其他”值不适合该枚举。
More quotes from the RFC 2616:
更多来自RFC 2616 的引用:
Practical information systems require more functionality than simple retrieval, including search, front-end update, and annotation. HTTP allows an open-ended set of methodsand headers that indicate the purpose of a request
实用的信息系统需要比简单的检索更多的功能,包括搜索、前端更新和注释。HTTP 允许使用一组开放式方法和标头来指示请求的目的
and,
和,
The Method token indicates the method to be performed on the resource identified by the Request-URI. The method is case-sensitive.
Method 标记指示要在由 Request-URI 标识的资源上执行的方法。该方法区分大小写。
Method = "OPTIONS" ; Section 9.2
| "GET" ; Section 9.3
| "HEAD" ; Section 9.4
| "POST" ; Section 9.5
| "PUT" ; Section 9.6
| "DELETE" ; Section 9.7
| "TRACE" ; Section 9.8
| "CONNECT" ; Section 9.9
| extension-method
extension-method = token
回答by Ben
The spec explicitly allows for more methods to be used, and so the set of all methods cannot be enumerated.
规范明确允许使用更多方法,因此无法枚举所有方法的集合。
回答by Erik Funkenbusch
As Damien mentions, RFC2616 only defines the commonmethods. HTTP, like XML, is a protocol that can be extended to support other formats.
正如 Damien 提到的,RFC2616 只定义了常用方法。HTTP 与 XML 一样,是一种可以扩展以支持其他格式的协议。
For instance, suppose I wanted to implement a special method called "Encrypt". If the HTTP library were enums, it would fail and likely throw an exception. Of course the client would have to know about this special request type, which is why most extensions are done via headers rather than commands.
例如,假设我想实现一种称为“加密”的特殊方法。如果 HTTP 库是枚举,它将失败并可能引发异常。当然,客户端必须知道这种特殊的请求类型,这就是为什么大多数扩展是通过标头而不是命令来完成的。
HTTP is an extensible protocol, but few people actually do extend it.
HTTP 是一种可扩展的协议,但很少有人真正扩展它。
Consider this simple example:
考虑这个简单的例子:
<form method="Foo" action="http://someurl"></form>
Since "method" is just text, and the user could put anything there, then the HTTP handler has to be able to handle it, correct?
由于“方法”只是文本,用户可以在其中放置任何内容,因此 HTTP 处理程序必须能够处理它,对吗?
EDIT:
编辑:
As it turns out, the HTML 4 specification only allows for GET and POST to be valid values, but HTTP goes beyond that.
事实证明,HTML 4 规范只允许 GET 和 POST 为有效值,但 HTTP 超出了这个范围。
回答by Ajay Bhosale
if HTTP Comes with a new method, then java and C# needs to update their enum. When will they update it? Will they release a patch? or will be updated in next version? So defining a enum on values which they dont control is not a wise decision.
如果 HTTP 带有新方法,那么 java 和 C# 需要更新它们的 enum。他们什么时候更新?他们会发布补丁吗?还是会在下个版本更新?因此,在他们无法控制的值上定义枚举并不是一个明智的决定。