📜  HTTP-请求

📅  最后修改于: 2020-12-14 05:20:33             🧑  作者: Mango


HTTP客户端以请求消息的形式向服务器发送HTTP请求,该消息包括以下格式:

  • 要求专线
  • 零个或多个标头(General | Request | Entity)字段,后跟CRLF
  • 空行(即CRLF之前没有任何内容的行)指示标头字段的结尾
  • 可选的消息正文
  • 以下各节说明了HTTP请求消息中使用的每个实体。

    请求线

    请求行以方法令牌开头,然后是请求URI和协议版本,以CRLF结尾。元素由空格SP字符分隔。

Request-Line = Method SP Request-URI SP HTTP-Version CRLF

让我们讨论请求行中提到的每个部分。

申请方法

请求方法表示要对由给定Request-URI标识的资源执行的方法。该方法区分大小写,应始终以大写形式提及。下表列出了HTTP / 1.1中所有受支持的方法。

S.N. Method and Description
1 GET

The GET method is used to retrieve information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data.

2 HEAD

Same as GET, but it transfers the status line and the header section only.

3 POST

A POST request is used to send data to the server, for example, customer information, file upload, etc. using HTML forms.

4 PUT

Replaces all the current representations of the target resource with the uploaded content.

5 DELETE

Removes all the current representations of the target resource given by URI.

6 CONNECT

Establishes a tunnel to the server identified by a given URI.

7 OPTIONS

Describe the communication options for the target resource.

8 TRACE

Performs a message loop back test along with the path to the target resource.

请求URI

Request-URI是统一资源标识符,用于标识在其上应用请求的资源。以下是最常用的形式来指定URI:

Request-URI = "*" | absoluteURI | abs_path | authority
S.N. Method and Description
1 The asterisk * is used when an HTTP request does not apply to a particular resource, but to the server itself, and is only allowed when the method used does not necessarily apply to a resource. For example:

OPTIONS * HTTP/1.1

2 The absoluteURI is used when an HTTP request is being made to a proxy. The proxy is requested to forward the request or service from a valid cache, and return the response. For example:

GET http://www.w3.org/pub/WWW/TheProject.html HTTP/1.1

3 The most common form of Request-URI is that used to identify a resource on an origin server or gateway. For example, a client wishing to retrieve a resource directly from the origin server would create a TCP connection to port 80 of the host “www.w3.org” and send the following lines:

GET /pub/WWW/TheProject.html HTTP/1.1

Host: www.w3.org

Note that the absolute path cannot be empty; if none is present in the original URI, it MUST be given as “/” (the server root).

请求标头字段

当我们学习HTTP标头字段时,我们将在单独的章节中研究General-header和Entity-header。现在,让我们检查一下“请求标头”字段是什么。

请求标头字段允许客户端将有关请求以及有关客户端本身的其他信息传递给服务器。这些字段用作请求修饰符。这里是一些重要的Request-header字段的列表,可以根据需求使用这些字段:

  • 接受字符集

  • 接受编码

  • 接受语言

  • 授权书

  • 期望

  • 主办

  • 如果匹配

  • 如果自修改

  • 如果不匹配

  • 如果范围

  • 如果自修改以来

  • 最大前进

  • 代理授权

  • 范围

  • 推荐人

  • TE

  • 用户代理

如果要编写自己的自定义客户端和Web服务器,则可以引入自定义字段。

请求消息的示例

现在,我们将它们放在一起以形成一个HTTP请求,以从在tutorialspoint.com上运行的Web服务器获取hello.htm页面。

GET /hello.htm HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

在这里,我们没有向服务器发送任何请求数据,因为我们正在从服务器获取纯HTML页面。连接是一个通用头,其余的头是请求头。下面的示例演示如何使用请求消息正文将表单数据发送到服务器:

POST /cgi-bin/process.cgi HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Content-Type: application/x-www-form-urlencoded
Content-Length: length
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

licenseID=string&content=string&/paramsXML=string

此处,给定的URL /cgi-bin/process.cgi将用于处理传递的数据,并相应地返回响应。这里content-type告诉服务器所传递的数据是简单的Web表单数据,而length将是放入消息正文中的数据的实际长度。以下示例显示如何将纯XML传递到Web服务器:

POST /cgi-bin/process.cgi HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive


string