gpt4 book ai didi

java - 为什么 HttpRequest.HttpMethod 是字符串而不是枚举?

转载 作者:IT老高 更新时间:2023-10-28 20:32:57 26 4
gpt4 key购买 nike

Reference of HttpRequest.HttpMethod在 .NET Framework 中,请求类型声明为 System.String 类型。

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

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

Java 在 HttpURLConnection#setRequestMethod(String) 上有类似的方法方法。

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

你有什么想法吗?

最佳答案

你的 RFC 2616 的第一句话链接(强调):

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

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

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

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


来自 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

和,

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

关于java - 为什么 HttpRequest.HttpMethod 是字符串而不是枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6722248/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com