English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
两种最常用的 HTTP 方法是:GET 和 POST。
超文本传输协议(HTTP)的设计目的是保证客户端与服务器之间的通信。
HTTP 的工作方式是客户端与服务器之间的请求-应答协议。
web 浏览器可能是客户端,而计算机上的网络应用程序也可能作为服务器端。
举例:客户端(浏览器)向服务器提交 HTTP 请求;服务器向客户端返回响应。响应包含关于请求的状态信息以及可能被请求的内容。
在客户机和服务器之间进行请求-响应时,两种最常被用到的方法是:GET 和 POST。
GET - 从指定的资源请求数据。
POST - 向指定的资源提交要被处理的数据。
请注意,查询字符串(名称/值对)是在 GET 请求的 URL 中发送的:
/run/demo-form.php?name1=value1&name2=value2
有关 GET 请求的其他一些注释:
GET 请求可被缓存
GET 请求保留在浏览器历史记录中
GET 请求可被收藏为书签
GET 请求不应在处理敏感数据时使用
GET 请求有长度限制
GET 请求只应当用于取回数据
请注意,查询字符串(名称/值对)是在 POST 请求的 HTTP 消息主体中发送的:
POST /run/demo-form.php HTTP/1.1
Host: oldtoolbag.com
name1=value1&name2=value2
有关 POST 请求的其他一些注释:
POST 请求不会被缓存
POST 请求不会保留在浏览器历史记录中
POST 不能被收藏为书签
POST 请求对数据长度没有要求
下面的表格比较了两种 HTTP 方法:GET 和 POST。
GET | POST | |
---|---|---|
后退按钮/刷新 | 无害 | 数据会被重新提交(浏览器应该告知用户数据会被重新提交)。 |
书签 | 可收藏为书签 | 不可收藏为书签 |
缓存 | 能被缓存 | 不能缓存 |
编码类型 | application/x-www-form-urlencoded | application/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 types | Only allows ASCII characters. | No restrictions. Also allows binary data. |
Security | Compared 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. |
Visibility | Data is visible to everyone in the URL. | Data will not be displayed in the URL. |
The following table lists some other HTTP request methods:
Method | Description |
---|---|
HEAD | Same as GET, but only returns HTTP headers, not the document body. |
PUT | Upload the specified URI representation. |
DELETE | Delete the specified resource. |
OPTIONS | Return the HTTP methods supported by the server. |
CONNECT | Convert the request connection to a transparent TCP/IP Channel. |