为什么Htt的prequest.HttpMethod是字符串,而不是枚举? [英] Why HttpRequest.HttpMethod is string instead of Enum?

查看:638
本文介绍了为什么Htt的prequest.HttpMethod是字符串,而不是枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的Htt的参考prequest.HttpMethod .NET框架,请求类型与 System.String 声明的类型。

2616 所有HTTP请求方法声明(如POST,GET, PUT,DELETE ...)。

还有类似的行为在<一个href="http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx"><$c$c>HttpWebRequest的WebRequest .NET类的。

Java的对类似的方法<一href="http://download.oracle.com/javase/6/docs/api/java/net/HttpURLConnection.html#setRequestMethod%28java.lang.String%29"><$c$c>HttpURLConnection#setRequestMethod(String)方法。

为什么这些语言的设计者没有考虑实施一个枚举那些HTTP方法?

你有一个想法?

解决方案

2616 链接(强调):

  

该组通用方法HTTP / 1.1定义如下。虽然的这一套可扩展 ...

也就是说,在HTTP的方法可以是任何东西。有众所周知或常用的方法,对这些很好理解的语义(好吧,好吧,的很好理解 - 我还是会遇到的人不清楚GET / POST)。

但是,任何应用程序可以实现其他方法。我们希望,这些其它方法的语义将被很好客户和服务器应用之间的理解。

有关这些原因,枚举是不合适的,因为总是可以将不适合在该枚举其他的值。


更多报价从 2616

  

实用的信息系统需要比简单的更多的功能      检索,包括搜索,前端更新及注解。 HTTP      允许开放式的方法集并标头指示      请求的目的

  

的方法标记表示方法中所执行的      资源由Request-URI中。该方法是区分大小写的。

 方法=选项;第9.2节
                  | 得到                    ; 9.3节
                  | 头                   ;第9.4节
                  | POST; 9.5节
                  | 放                    ; 9.6节
                  | 删除                 ;第9.7节
                  | 跟踪                  ;第9.8节
                  | CONNECT;第9.9节
                  |扩展法
   扩展法=令牌
 

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?

解决方案

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

这篇关于为什么Htt的prequest.HttpMethod是字符串,而不是枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆