English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML Referenzhandbuch

HTML-Tag-Liste

HTTP-Methoden GET und POST

两种最常用的 HTTP 方法是:GET 和 POST。

什么是 HTTP ?

超文本传输协议(HTTP)的设计目的是保证客户端与服务器之间的通信。

HTTP 的工作方式是客户端与服务器之间的请求-应答协议。

web 浏览器可能是客户端,而计算机上的网络应用程序也可能作为服务器端。

举例:客户端(浏览器)向服务器提交 HTTP 请求;服务器向客户端返回响应。响应包含关于请求的状态信息以及可能被请求的内容。

两种 HTTP 请求方法:GET 和 POST

在客户机和服务器之间进行请求-响应时,两种最常被用到的方法是:GET 和 POST。

  • GET - 从指定的资源请求数据。

  • POST - 向指定的资源提交要被处理的数据。

GET 方法

请注意,查询字符串(名称/值对)是在 GET 请求的 URL 中发送的:

/run/demo-form.php?name1=value1&name2=value2

有关 GET 请求的其他一些注释:

  • GET 请求可被缓存

  • GET 请求保留在浏览器历史记录中

  • GET 请求可被收藏为书签

  • GET 请求不应在处理敏感数据时使用

  • GET 请求有长度限制

  • GET 请求只应当用于取回数据

POST 方法

请注意,查询字符串(名称/值对)是在 POST 请求的 HTTP 消息主体中发送的:

POST /run/demo-form.php HTTP/1.1
Host: oldtoolbag.com
name1=value1&name2=value2

有关 POST 请求的其他一些注释:

  • POST 请求不会被缓存

  • POST 请求不会保留在浏览器历史记录中

  • POST 不能被收藏为书签

  • POST 请求对数据长度没有要求

比较 GET 与 POST

下面的表格比较了两种 HTTP 方法:GET 和 POST。

 GETPOST
后退按钮/刷新无害数据会被重新提交(浏览器应该告知用户数据会被重新提交)。
书签可收藏为书签不可收藏为书签
缓存能被缓存不能缓存
编码类型application/x-www-form-urlencodedapplication/x-www-form-urlencoded or multipart/form-data。为二进制数据使用多重编码。
历史参数保留在浏览器历史中。参数不会保存在浏览器历史中。
限制数据长度Yes. When sending data, the GET method adds data to the URL; the length of the URL is limited (the maximum length of the URL is 2048 characters).Unrestricted.
Restrictions on data typesOnly allows ASCII characters.No restrictions. Also allows binary data.
SecurityCompared to POST, GET has poorer security because the data sent is part of the URL.
Never use GET when sending passwords or other sensitive information!
POST is more secure than GET because parameters are not saved in the browser history or web server logs.
VisibilityData is visible to everyone in the URL.Data will not be displayed in the URL.

Other HTTP request methods

The following table lists some other HTTP request methods:

MethodDescription
HEADSame as GET, but only returns HTTP headers, not the document body.
PUTUpload the specified URI representation.
DELETEDelete the specified resource.
OPTIONSReturn the HTTP methods supported by the server.
CONNECTConvert the request connection to a transparent TCP/IP Channel.