什么时候用GET?什么时候用POST?

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。

一、

GET和POST两种方法都是将数据送到服务器,但你该用哪一种呢?

HTTP标准包含这两种方法是为了达到不同的目的。POST用于创建资源,资源的内容会被编入HTTP请示的内容中。例如,处理订货表单、在数据库中加入新数据行等。

当请求无副作用时(如进行搜索),便可使用GET方法;当请求有副作用时(如添加数据行),则用POST方法。一个比较实际的问题是:GET方法可能会产生很长的URL,或许会超过某些浏览器与服务器对URL长度的限制。

若符合下列任一情况,则用POST方法:

* 请求的结果有持续性的副作用,例如,数据库内添加新的数据行。
* 若使用GET方法,则表单上收集的数据可能让URL过长。
* 要传送的数据不是采用7位的ASCII编码。

若符合下列任一情况,则用GET方法:

* 请求是为了查找资源,HTML表单数据仅用来帮助搜索。
* 请求结果无持续性的副作用。
* 收集的数据及HTML表单内的输入字段名称的总长不超过1024个字符。

 


二、

最近开到一段W3的资料,写的不错,原文地址如下:http://bu-choreography.iteye.com/admin/blogs/new。翻译总结如下: 
快速判断: 
如下情况使用GET方法:客户端与服务端的交互像是一个提问(如查询操作、搜索操作、读操作) 
如下情况使用POST方法: 
       1.交互是一个命令或订单(order),比提问包含更多信息 
       2.交互改变了服务器端的资源并被用户察觉,例如订阅某项服务 
       3.用户需要对交互产生的结果负责 
听起来稍微明白了一点,接着来。 
根据HTTP协议规定,GET方法可以携带交互需要的所有数据,因此你会看到搜索百度或谷歌的时候,点击搜索形成的URL包含了你刚才的搜索关键字,没有安全需求的请求把信息放URL里没关系,但是你访问银行网站的时候,不希望把账户、密码这些放在URL里被人拦截是吧,所以HTTP设计了POST请求,他可以把请求信息放在HTTP请求里,具体格式这里不细说了,这样你就不能简单的从URL里找到账户、密码了。 
讲完这些,是不是比较清楚了呢。 
文章还例举了几个曾经的HTTP请求限制。 
1.URI不能超过256个字符。这个限制在有些服务器里是存在的,有的服务器为了网络安全,为了防止拒绝式攻击会把URL字符限制在4000字符 
2.你提交了GET请求,又马山按了backspace键,会导致get方法被重新执行 
3.你在一个页面使用了安全协议,跳转到了另一个使用不安全协议的页面时,会导致安全数据泄漏给第二个页面。

 


以上一中内容摘自《Web Database Application with PHP and MySQL, 2nd Edition》一书,中文版《PHP & MySQL Web数据库应用开发指南》。英文原文内容如下: 
GET versus POST

Both the GET and POST methods send data to the server, but which method should you use?

The HTTP standard includes the two methods to achieve different goals. The POST method was intended to create a resource. The contents of the resource would be encoded into the body of the HTTP request. For example, an order form might be processed and a new row in a database created.

The GET method is used when a request has no side effects (such as performing a search) and the POST method is used when a request has side effects (such as adding a new row to a database). A more practical issue is that the GET method may result in long URLs, and may even exceed some browser and server limits on URL length.

Use the POST method if any of the following are true:

* The result of the request has persistent side effects such as adding a new database row.
* The data collected on the form is likely to result in a long URL if you used the GET method.
* The data to be sent is in any encoding other than seven-bit ASCII.

Use the GET method if all the following are true:

* The request is to find a resource, and HTML form data is used to help that search.
* The result of the request has no persistent side effects.
* The data collected and the input field names in a HTML form are in total less than 1,024 characters in size.

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值