Hands-On Network Programming with C# and .NET Core
上QQ阅读APP看书,第一时间看更新

Request methods

When a client wants to make a request of a server, it must specify the method by which the server will be expected to respond to the given request. These method specifications are typically called HTTP verbs, since most of them describe an action to be taken by the server when processing a request sent by the client. The standard methods are as follows:

  • OPTIONS: This returns the list of other HTTP methods supported by the server at the given URL.
  • TRACE: This is a utility method that will simply echo the original request as received by the server. It is useful for identifying any modifications made to the request by entities on the network while the request is in transit.
  • CONNECT: CONNECT requests establish a transparent TCP/IP tunnel between the originating host and the remote host.
  • GET: This retrieves a copy of the resource specified by the URL to which the HTTP request was sent. By convention, GET requests will only ever retrieve the resource, with no side-effects on the state of the resources on the server (however, these conventions can be broken by poor programming practices, as we'll see later in the book).
  • HEAD: This method requests the same response as a GET request to a given URL, but without the body of the response. What is returned is only the response headers.
  • POST: The POST method transmits data in the body of the request, and requests that the server store the content of the request body as a new resource hosted by the server.
  • PUT: The PUT method is similar to the POST method in that the client is requesting that the server store the content of the request body. However, in the case of a PUT operation, if there is already content at the requested URL, this content is then modified and updated with the contents of the request body.
  • PATCH: The PATCH method will perform partial updates of the resource at the requested URL, modifying it with the contents of the request body. A PATCH request will typically fail if there is no resource already on the server to be patched.
  • DELETE: The DELETE method will permanently delete the resource at the specified URL.

A server will not respond to a request method invoked against a given location unless the server has been configured to do so. This is because some of the methods defined by the HTTP standard can permanently impact the state of resources on that server, and so should only be invoked and processed when it is safe to irrevocably update that state. There are, however, a number of methods designated as safe, by convention. This simply means that they can be processed by a server without having any side-effects on the state of the resources on that server. HEAD, GET, OPTIONS, and TRACE are all conventionally designated as safe.