HTTP Request 常用方法GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE

HTTP Request GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE Methods

注:本文为个人学习摘录,原文地址为:http://javaeedevelop.iteye.com/blog/1725299

 

An HTTP request is a class consisting of HTTP style requests, request lines, request methods, request URL, header fields, and body content. The most common methods that are used by a client in an HTTP request are as follows:-

1) GET:- Used when the client is requesting a resource on the Web server.

    当客户端向Web服务器请求一个资源的时候使用。


2) HEAD:- Used when the client is requesting some information about a resource but not requesting the resource itself.

    当客户端向Web服务器请求一个资源的一些信息而不是资源的全部信息的时候使用。

3) POST:- Used when the client is sending information or data to the server—for example, filling out an online form (i.e. Sends a large amount of complex data to the Web Server).

    当客户端向服务端发送信息或者数据的时候使用--例如,表单提交(向Web服务器发送大量的复杂的数据)。

4) PUT:- Used when the client is sending a replacement document or uploading a new document to the Web server under the request URL.

    当客户端向Web服务端指定URL发送一个替换的文档或者上传一个新万当的时候使用。

5) DELETE:- Used when the client is trying to delete a document from the Web server, identified by the request URL.

    当客户端尝试从Web服务端删除一个由请求URL唯一标识的文档的时候使用。

6) TRACE:- Used when the client is asking the available proxies or intermediate servers changing the request to announce themselves.

    当客户端要求可用的代理服务器或者中间服务更改请求来宣布自己的时候使用。

7) OPTIONS:- Used when the client wants to determine other available methods to retrieve or process a document on the Web server. 

    当客户端想要决定其他可用的方法来检索或者处理Web服务端的一个文档时使用。

8) CONNECT:- Used when the client wants to establish a transparent connection to a remote host, usually to facilitate SSL-encrypted communication (HTTPS) through an HTTP proxy.

    当客户端想要确定一个明确的连接到远程主机的时候使用,通常便于通过Http代理服务器进行SSL加密通信(Https)连接使用。

特定的HTTP服务器还能够扩展自定义的方法

9) PATCH:-用于将局部修改应用到资源。

 

1、The GET Request Method --Get请求方法


The GET method is the simplest and the most frequently used request method. It is used to access the static resources, such as HTML documents and images. GET request can be used to retrieve dynamic information by including query parameters in the request URL. For instance, we can send a parameter name with the URL, such as http://www.domain.com?name=Harsh. In this example, Harsh is the dynamic information sent by including a parameter,name, in the request URL. The Web Server can then access this dynamic information through the “name” parameter.

Get方法是最简单最常用的请求方法。它被用来访问静态资源,比如HTML文档和图片等。Get请求可以通过在请求URL中包含查询参数的方法来检索动态信息。例如,我们可以通过URL发送“name”这个参数(http://www.domain.com?name=Harsh),在这个例子中,Harsh是通过在请求URL中包含参数“name”的动态的信息。Web服务可以通过这个“name”参数来访问动态信息。



2、The HEAD Request Method -- Head方法

According to Wikipedia “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.” It is used when the client is requesting some information about a resource but not requesting the resource itself. This means that we have a faster way of checking the headers and some server info for a given resource on the server i.e. checking if a given url is serviceable, a given file exists, etc..Sometimes client might only need to view the header of a response (Content-Type or Content-Length). The client can use the HEAD request method to retrieve the header in such cases. The HEAD method is similar to GET method, except that the server does not return a message body (actual page) in response of the HEAD method.

根据维基百科“要求响应上等同于Get请求,但是没有响应Body(Response Body)。这在检索写在响应头(Response Header)中的信息而不需要传输全部的内容的情况下非常有用。”当客户端请求关于一个资源的一些信息而不是请求全部资源的时候使用Head方法。这意味着我们有一种快速的方式来检查针对服务端上一个给定的资源的头信息和一些服务端信息,检测一个给定的url是否可用、给定的文件是否存在等等。有时客户端仅仅需要查看相应的头信息(Content-Type或者Content-Length)。在此类案例中客户端可以使用HEAD请求方法来检测头信息。HEAD方法与GET方法类似,但是HEAD方法不希望在响应信息中包含Message Body(实际页面)。

3、The POST Request Method--Post方法

The Post method is commonly used for accessing dynamic resources or when a large amount of complex information is to be sent to the server. The Web Server accepts the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI( Uniform Resource Identifier). According to Wikipedia “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.” The major difference between GET and POST is that in GET the request parameters are transmitted as a query string appended to the request URL, while in POST the request parameters are transmitted within the body of the request.

Post方法一般用来访问动态资源,或者在向服务端发送大量的复杂信息的时候使用。Web服务器接收封装在Request中的实体,作为请求URI唯一标识的资源的附属。根据维基百科“提交数据被处理到唯一标识资源。数据被包含在请求的Body(Request Body)中。这可能会导致创建一个新的资源或者更新已有的资源或者两者兼之。”Get方法和Post方法的主要区别是:在Get方法请求中,请求参数是附加在请求URL最后来传输的,而在Post方法中,请求参数是被包含在请求体(Request Body)中传输的。

The POST request method provides the following functionalities:
1) Providing annotations of the existing resources.
2) Posting a message to a bulletin board, newsgroup, mailing list, or a similar group of articles.
3) Providing a block of data, such as the result of the submitting a form, to a data-handling process.
4) Extending a database through an append operation.

Post请求方法提供了如下功能:

(1)为存在的资源提供注释。

(2)发布信息给公告牌、新闻组、邮件列表或者一组类似的文章。

(3)提供一块数据,例如提交Form表单到数据处理单元的结果。

(4)通过附加操作扩展数据库。



4、The PUT Request method--Put方法

The PUT method stores an entity in the specified Request-URI. The entity is a resource residing on the Web server under the specified Request-URI. If the Request-URI does not point to an existing resource, but is capable of being defined as a new resource by the requesting user, the Web Server can create the resource with that URI. If an existing resource is modified, either the 200(OK) or 204 (No Content) response code should be sent to indicate successful modification of a resource. The Web Server must inform the user via the 201 (Created) responses if a new resource is created. If the resource is not created or modified with the Request-URI, an appropriate error response is generated, which reflects the nature of the problem.

Put方法存储一个实体到指定的请求URI。该实体是Web服务端上指定请求URI下的资源。如果请求URI没有指向一个存在的资源,也能被发出请求的用户定义为一个新的资源,Web服务端将使用该URI创建资源。如果一个已经存在的资源被修改,200(OK)或者204(No Content)响应代码将被发送来表示成功修改资源。如果一个新的资源被创建,Web服务端必须向用户返回201(Created)相应代码。如果指定请求URI没有创建或者修改资源,将产生一个适当的错误响应,以反应问题的性质。



5、The DELETE Request method--Delete方法

The DELETE method requests the Web server to delete the resource identified by the Request-URI. This method may be overridden by human intervention (or other means) on the Web Server. If the response includes an entity describing the status of deletion, the 200(OK) response code specifies that the resource has been deleted successfully. If the response is 202(Accepted), it specifies that the resource has not yet been deleted. Similarly, if the response code is 204 ( No Content), it specifies that the resource has been deleted but the response code does not include an entity.

Delete方法通过请求URI请求Web服务删除指定资源。该方法可能在Web服务端被人为的重写。如果响应信息中包含一个实体描述删除的状态,200(OK)相应代码指出资源已经被成功删除。如果响应代码是202(Accepted),它指出资源还没有被删除。相似的,如果相应代码是204(No Content),它指出资源已经被删除但是响应代码不包含任何实体。

6、The OPTIONS Request method--Options方法

According to Wikipedia “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.”
The OPTION method requests for information about the communication options available on the request/response chain identified by a Request-URI. Responses to this method are not cacheable. This method allows the client to determine the options and /or requirements associated with a resource, or the capabilities of a server. If the OPTIONS method includes an entity body, the media type must be indicated by the content-type field.

根据维基百科“返回服务端针对指定的URL所支持的HTTP请求方法。用“*”代替资源名称,想Web服务器发送Options请求,可以测试服务器功能是否正常运作。"Options方法请求关于可请求URI所表示的请求/响应链上的用的通信选项的信息。该方法的响应信息不能被缓存。该方法允许客户端决定选项和/或要求与一种资源或者服务的功能相关联。如果Options方法包含一个实体,则media类型必须通过Content-Type字段来标明。


7、The TRACE Request method--Trace方法

According to Wikipedia “Echoes back the received request, so that a client can see what (if any) changes or additions have been made by intermediate servers.”
The TRACE method is used to invoke a remote application layer associated with a request message. A TRACE request must not include an entity. A client uses the TRACE method to see the received input at the other end of the request chain and diagnostic or testing information.

根据维基百科“回显服务器收到的请求,这样客户端可以看到(如果有)哪一些改变或者添加已经被中间服务器实现。”Trace方法被用来调用与请求消息相关联的远程应用程序层。Trace请求必须包含一个实体。客户端使用Trace方法看到接收用户输入的请求链和诊断或检测信息的另一端。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. GET方法 ```csharp using System.Net.Http; // 创建HttpClient对象 HttpClient client = new HttpClient(); // 发送GET请求 HttpResponseMessage response = await client.GetAsync("http://www.example.com"); string responseBody = await response.Content.ReadAsStringAsync(); ``` 2. POST方法 ```csharp using System.Net.Http; using System.Text; using System.Text.Json; // 创建HttpClient对象 HttpClient client = new HttpClient(); // 准备POST请求数据 var data = new { name = "John", age = 30 }; var jsonContent = JsonSerializer.Serialize(data); var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); // 发送POST请求 HttpResponseMessage response = await client.PostAsync("http://www.example.com", content); string responseBody = await response.Content.ReadAsStringAsync(); ``` 3. PUT方法 ```csharp using System.Net.Http; using System.Text; using System.Text.Json; // 创建HttpClient对象 HttpClient client = new HttpClient(); // 准备PUT请求数据 var data = new { name = "John", age = 30 }; var jsonContent = JsonSerializer.Serialize(data); var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); // 发送PUT请求 HttpResponseMessage response = await client.PutAsync("http://www.example.com", content); string responseBody = await response.Content.ReadAsStringAsync(); ``` 4. DELETE方法 ```csharp using System.Net.Http; // 创建HttpClient对象 HttpClient client = new HttpClient(); // 发送DELETE请求 HttpResponseMessage response = await client.DeleteAsync("http://www.example.com"); string responseBody = await response.Content.ReadAsStringAsync(); ``` 5. HEAD方法 ```csharp using System.Net.Http; // 创建HttpClient对象 HttpClient client = new HttpClient(); // 发送HEAD请求 HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Head, "http://www.example.com"); HttpResponseMessage response = await client.SendAsync(request); ``` 6. OPTIONS方法 ```csharp using System.Net.Http; // 创建HttpClient对象 HttpClient client = new HttpClient(); // 发送OPTIONS请求 HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Options, "http://www.example.com"); HttpResponseMessage response = await client.SendAsync(request); ``` 7. TRACE方法 ```csharp using System.Net.Http; // 创建HttpClient对象 HttpClient client = new HttpClient(); // 发送TRACE请求 HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Trace, "http://www.example.com"); HttpResponseMessage response = await client.SendAsync(request); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值