为什么 HttpRequest.HttpMethod 是字符串而不是 Enum?

发布于 2024-11-24 16:19:55 字数 777 浏览 2 评论 0原文

HttpRequest.HttpMethod 的参考在.NET Framework 中,请求类型是用System.String 类型声明的。

RFC 2616 中声明了所有 HTTP 请求方法(例如 POST、GET、PUT、DELETE ...)。

HttpWebRequest中也有类似的行为.NET 的 >WebRequest 类。

Java 在 HttpURLConnection#setRequestMethod(String) 方法。

为什么这些语言设计者不考虑为这些 HTTP 方法实现枚举?

你有主意吗?

In the Reference of HttpRequest.HttpMethod of .NET Framework, request type is declared with System.String type.

In RFC 2616 all HTTP request methods are declared (e.g. POST, GET, PUT, DELETE...).

There's also similar behavior in HttpWebRequest and WebRequest classes of .NET.

Java has the similar approach on HttpURLConnection#setRequestMethod(String) method.

Why do these language designers do not consider implementing an enum for those HTTP methods?

Do you have an idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陈甜 2024-12-01 16:19:55

RFC 2616 链接的第一句(已添加重点):

HTTP/1.1 的一组通用方法定义如下。虽然这个集合可以扩展...

也就是说,HTTP中的方法可以是任何东西。有一些“众所周知的”或常见的方法,其语义很好理解(好吧,应该很好理解 - 我仍然遇到对 GET/POST 不清楚的人)。

但任何应用程序都可以实现其他方法。希望客户端和服务器应用程序能够很好地理解这些其他方法的语义。

由于这些原因,枚举是不合适的,因为总是可能存在不适合该枚举的“其他”值。


更多来自 RFC 2616 的引用:

实用的信息系统需要的功能不仅仅是简单的
检索,包括搜索、前端更新、标注。 HTTP
允许一组开放式的方法和标头来指示
请求的目的


,以及

方法标记指示要在
由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

The first sentences of your RFC 2616 link (emphasis added):

The set of common methods for HTTP/1.1 is defined below. Although this set can be expanded...

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, should be well understood - I still encounter people unclear on 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:

Practical information systems require more functionality than simple
retrieval, including search, front-end update, and annotation. HTTP
allows an open-ended set of methods
and headers that indicate the
purpose of a request

and,

The Method token indicates the method to be performed on the
resource identified by the Request-URI. The method is case-sensitive.

   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
稀香 2024-12-01 16:19:55

该规范明确允许使用更多方法,因此无法枚举所有方法的集合。

The spec explicitly allows for more methods to be used, and so the set of all methods cannot be enumerated.

桃气十足 2024-12-01 16:19:55

如果 HTTP 带有新方法,那么 java 和 C# 需要更新它们的枚举。他们什么时候更新?他们会发布补丁吗?或者会在下一个版本更新?因此,定义一个他们无法控制的值的枚举并不是一个明智的决定。

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.

盗梦空间 2024-12-01 16:19:55

正如 Damien 提到的,RFC2616 仅定义了通用方法。 HTTP 与 XML 一样,是一种可以扩展以支持其他格式的协议。

例如,假设我想实现一个名为“Encrypt”的特殊方法。如果 HTTP 库是枚举,它将失败并可能引发异常。当然,客户端必须了解这种特殊的请求类型,这就是为什么大多数扩展都是通过标头而不是命令来完成的。

HTTP 是一个可扩展的协议,但很少有人真正扩展它。

考虑这个简单的例子:

<form method="Foo" action="http://someurl"></form>

由于“方法”只是文本,并且用户可以在其中放置任何内容,那么 HTTP 处理程序必须能够处理它,对吗?

编辑:

事实证明,HTML 4 规范只允许 GET 和 POST 为有效值,但 HTTP 超出了这一范围。

As Damien mentions, RFC2616 only defines the common methods. HTTP, like XML, is a protocol that can be extended to support other formats.

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 is an extensible protocol, but few people actually do extend it.

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?

EDIT:

As it turns out, the HTML 4 specification only allows for GET and POST to be valid values, but HTTP goes beyond that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文