HttpRequest的HttpMethod和RequestType有什么区别?

发布于 2024-08-19 20:49:57 字数 1492 浏览 7 评论 0原文

HttpRequest 类定义了两个属性:

< a href="http://msdn.microsoft.com/en-us/library/system.web.httprequest.httpmethod.aspx" rel="noreferrer">HttpMethod:

获取客户端使用的HTTP数据传输方法(例如GET、POST或HEAD)。

public string HttpMethod { get; }  

客户端使用的HTTP数据传输方式。

RequestType :

获取或设置客户端使用的HTTP数据传输方法(GET或POST)。

公共字符串RequestType { get;放; }

表示客户端发送的 HTTP 调用类型的字符串。

这两个属性有什么区别?我什么时候想使用其中一种而不是另一种?哪个是正确的检查方式以了解客户端使用了哪种数据传输方法?

文档表明 HttpMethod 将返回使用的任何动词:

例如 GET、POST 或 HEAD

,而有关 RequestType 的文档似乎仅指示两个可能值之一:

获取或发布


测试,这两个属性似乎都支持所有动词,并且都返回相同的值:

测试:

Client Used    HttpMethod    RequestType
GET            GET           GET
POST           POST          POST
HEAD           HEAD          HEAD
CONNECT        CONNECT       CONNECT
MKCOL          MKCOL         MKCOL
PUT            PUT           PUT
FOOTEST        FOOTEST       FOOTEST

之间有什么区别

  • HttpRequest.HttpMethod
  • HttpRequest.RequestType

以及何时应该使用一个其他?

The HttpRequest class defines two properties:

HttpMethod:

Gets the HTTP data transfer method (such as GET, POST, or HEAD) used by the client.

public string HttpMethod { get; }  

The HTTP data transfer method used by the client.

and RequestType:

Gets or sets the HTTP data transfer method (GET or POST) used by the client.

public string RequestType { get; set; }

A string representing the HTTP invocation type sent by the client.

What is the difference between these two properties? When would I want to use one over the other? Which is the proper one to inspect to see what data transfer method was used by the client?

The documentation indicates that HttpMethod will return whatever verb was used:

such as GET, POST, or HEAD

while the documentation on RequestType seems to indicate only one of two possible values:

GET or POST


I tested with a random sampling of verbs, and both properties seem to support all verbs, and both return the same values:

Testing:

Client Used    HttpMethod    RequestType
GET            GET           GET
POST           POST          POST
HEAD           HEAD          HEAD
CONNECT        CONNECT       CONNECT
MKCOL          MKCOL         MKCOL
PUT            PUT           PUT
FOOTEST        FOOTEST       FOOTEST

What is the difference between:

  • HttpRequest.HttpMethod
  • HttpRequest.RequestType

and when should I use one over the other?

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

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

发布评论

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

评论(2

神妖 2024-08-26 20:49:57

Reflector 显示 RequestType 调用 HttpMethod内部。因此,调用 HttpMethod稍微更好一些。实际上,我认为 RequestType 存在的真正原因是为了向后兼容经典 ASP。

Reflector shows that RequestType calls HttpMethod internally. So you're ever so slightly better off calling HttpMethod. Actually I think the real reason RequestType exists was for backwards compatibility with classic ASP.

爱*していゐ 2024-08-26 20:49:57

您可以查看以下文章:-

请求方法:
使用 telnet 发出的 HTTP 请求。请求、响应标头和响应正文突出显示。

HTTP 定义了八种方法(有时称为“动词”),指示要对所识别的资源执行的所需操作。该资源代表什么,无论是预先存在的数据还是动态生成的数据,取决于服务器的实现。通常,资源对应于驻留在服务器上的文件或可执行文件的输出。


请求与 GET 请求对应的响应相同的响应,但没有响应正文。这对于检索响应标头中写入的元信息非常有用,而无需传输整个内容。

得到
请求指定资源的表示。请注意,GET 不应用于会产生副作用的操作,例如使用它在 Web 应用程序中执行操作。原因之一是 GET 可能被机器人或爬虫任意使用,它们不应该考虑请求应引起的副作用。请参阅下面的安全方法。

邮政
将要处理的数据(例如,来自 HTML 表单)提交到所识别的资源。数据包含在请求正文中。这可能会导致创建新资源或更新现有资源或两者兼而有之。

上传指定资源的表示。
删除
删除指定的资源。
痕迹
回显收到的请求,以便客户端可以看到中间服务器在请求中添加或更改了哪些内容。
选项
返回服务器支持指定 URL 的 HTTP 方法。这可用于通过请求“*”而不是特定资源来检查 Web 服务器的功能。
连接
将请求连接转换为透明 TCP/IP 隧道,通常是为了通过未加密的 HTTP 代理进行 SSL 加密通信 (HTTPS)。 [5]
修补
用于对资源应用部分修改。[6]

HTTP 服务器需要至少实现 GET 和 HEAD 方法[7],并且尽可能实现 OPTIONS 方法。[需要引用]
安全方法

某些方法(例如 HEAD、GET、OPTIONS 和 TRACE)被定义为安全的,这意味着它们仅用于信息检索,不应更改服务器的状态。换句话说,除了相对无害的影响(例如日志记录、缓存、横幅广告服务或增加网络计数器)之外,它们不应该有副作用。因此,在不考虑应用程序状态上下文的情况下发出任意 GET 请求应该被认为是安全的。

相比之下,诸如 POST、PUT 和 DELETE 之类的方法适用于可能对服务器造成副作用或外部副作用(例如金融交易或电子邮件传输)的操作。因此,此类方法通常不被符合要求的网络机器人或网络爬虫使用,它们倾向于在不考虑上下文或后果的情况下提出请求。

尽管规定了 GET 请求的安全性,但实际上服务器对它们的处理在技术上不受任何方式的限制,并且粗心或故意的编程也很容易(或更容易,由于缺乏用户代理预防措施)导致不平凡的更改在服务器上。不鼓励这样做,因为它可能会导致 Web 缓存、搜索引擎和其他自动化代理出现问题,从而可能对服务器进行意外更改。

You can check below article:-

Request methods:
An HTTP request made using telnet. The request, response headers and response body are highlighted.

HTTP defines eight methods (sometimes referred to as "verbs") indicating the desired action to be performed on the identified resource. What this resource represents, whether pre-existing data or data that is generated dynamically, depends on the implementation of the server. Often, the resource corresponds to a file or the output of an executable residing on the server.

HEAD
Asks for the response identical to the one that would correspond to a GET request, but without the response body. This is useful for retrieving meta-information written in response headers, without having to transport the entire content.

GET
Requests a representation of the specified resource. Note that GET should not be used for operations that cause side-effects, such as using it for taking actions in web applications. One reason for this is that GET may be used arbitrarily by robots or crawlers, which should not need to consider the side effects that a request should cause. See safe methods below.

POST
Submits data to be processed (e.g., from an HTML form) to the identified resource. The data is included in the body of the request. This may result in the creation of a new resource or the updates of existing resources or both.
PUT
Uploads a representation of the specified resource.
DELETE
Deletes the specified resource.
TRACE
Echoes back the received request, so that a client can see what intermediate servers are adding or changing in the request.
OPTIONS
Returns the HTTP methods that the server supports for specified URL. This can be used to check the functionality of a web server by requesting '*' instead of a specific resource.
CONNECT
Converts the request connection to a transparent TCP/IP tunnel, usually to facilitate SSL-encrypted communication (HTTPS) through an unencrypted HTTP proxy.[5]
PATCH
Is used to apply partial modifications to a resource.[6]

HTTP servers are required to implement at least the GET and HEAD methods[7] and, whenever possible, also the OPTIONS method.[citation needed]
Safe methods

Some methods (for example, HEAD, GET, OPTIONS and TRACE) are defined as safe, which means they are intended only for information retrieval and should not change the state of the server. In other words, they should not have side effects, beyond relatively harmless effects such as logging, caching, the serving of banner advertisements or incrementing a web counter. Making arbitrary GET requests without regard to the context of the application's state should therefore be considered safe.

By contrast, methods such as POST, PUT and DELETE are intended for actions which may cause side effects either on the server, or external side effects such as financial transactions or transmission of email. Such methods are therefore not usually used by conforming web robots or web crawlers, which tend to make requests without regard to context or consequences.

Despite the prescribed safety of GET requests, in practice their handling by the server is not technically limited in any way, and careless or deliberate programming can just as easily (or more easily, due to lack of user agent precautions) cause non-trivial changes on the server. This is discouraged, because it can cause problems for Web caching, search engines and other automated agents, which can make unintended changes on the server.

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