HTTP Method 之 Post VS. Get
引言
WebAPI 现在非常火的轻量服务框架,因其使用得使用了Http协议,并且具备了可协商内容(生成不同内容格式)等优势,所以在互联网业务中被广泛使用。
那作为HTTP最常用的两个方法Get和Post 是什么,他们之间又有哪些区别?这一问题经常成为面试的考点,这里就做一个简短总结,不保证全,但保证对。
什么是HTTP?
Hypertext Transfer Protocol (HTTP)是为服务端客户和客户端通信而设计的一种请求响应(Request-Response)协议。
HTTP中最常的用两种请求方式:
- GET - Requests data from a specified resource (从指定的资源获取数据,资源可以简单认为就是服务器)
- POST - Submits data to be processed to a specified resource (向指定的资源提及要处理的数据。)
Get方法
Note that the query string (name/value pairs) is sent in the URL of a GET request:
注意 查询字符串(键值对)是在Get请求的URL中携带的。
/test/demo_form.asp?name1=value1&name2=value2
- GET requests can be cached (请求可以被缓存)
- GET requests remain in the browser history (请求会保留在浏览器的历史纪录)
- GET requests can be bookmarked (请求可以作为书签)
- GET requests should never be used when dealing with sensitive data (请求不应该用于处理敏感数据)
- GET requests have length restrictions (请求有相应的长度限制,虽然有网友说可以没有限制,但我还是同意协议制定方的约定)
- GET requests should be used only to retrieve data(请求应该仅用于获取数据)
补充,关于Get Request的请求长度
The limit length of Get Request is dependent on both the server and the client used (and if applicable, also the proxy the server or the client is using).
Most webservers have a limit of 8192 bytes (8KB), which is usually configureable somewhere in the server configuration. As to the client side matter, the HTTP 1.1 specification even warns about this, here's an extract of chapter 3.2.1:
Note: Servers ought to be cautious about depending on URI lengths above 255 bytes, because some older client or proxy implementations might not properly support these lengths.
Post方法
Note that the query string (name/value pairs) is sent in the HTTP message body of a POST request:
注意 查询字符串(键值对)是通过Post请求的HTTP消息体携带的。
POST /test/demo_form.asp HTTP/1.1
Host: w3schools.com
name1=value1&name2=value2
- POST requests are never cached (请求数据永远不会被缓存)
- POST requests do not remain in the browser history (请求数据不会保存到浏览器的历史纪录中)
- POST requests cannot be bookmarked (请求不能做浏览器书签)
- POST requests have no restrictions on data length(请求没有严格的数据长度限制)
Get VS. Post
GET | POST | |
---|---|---|
BACK button/Reload | Harmless(没有什么坏处,不过如果你没按约定做,Get做了更新或者删除,那就是自作孽) | Data will be re-submitted (the browser should alert the user that the data are about to be re-submitted)数据会被重新提交,浏览器应该对这种重新提交的数据向用户提出警告。 |
Bookmarked | Can be bookmarked | Cannot be bookmarked |
Cached | Can be cached | Not cached |
Encoding type | application/x-www-form-urlencoded | application/x-www-form-urlencoded or multipart/form-data. Use multipart encoding for binary data |
History | Parameters remain in browser history | Parameters are not saved in browser history |
Restrictions on data length | Yes, when sending data, the GET method adds the data to the URL; and the length of a URL is limited (maximum URL length is 2048 characters) | No restrictions |
Restrictions on data type | Only ASCII characters allowed(不允许中文,中文需自行编码处理) | No restrictions. Binary data is also allowed |
Security | GET is less secure compared to POST because data sent is part of the URL
Never use GET when sending passwords or other sensitive information! |
POST is a little safer than GET because the parameters are not stored in browser history or in web server logs |
Visibility | Data is visible to everyone in the URL | Data is not displayed in the URL |
如何选择呢?
POST和GET的差别其实是很大的。
语义上,GET是获取指定URL上的资源,是读操作,重要的一点是不论对某个资源GET多少次,它的状态是不会改变的,在这个意义上,我们说GET是安全的(不是被密码学或者数据保护意义上的安全)。因为GET是安全的,所以GET返回的内容可以被浏览器,Cache服务器缓存起来(其中还有很多细节,但不影响这里的讨论)。
而POST的语意是对指定资源“追加/添加”数据,所以是不安全的,每次提交的POST,参与的代码都会认为这个操作会修改操作对象资源的状态,于是,浏览器在你按下F5的时候会跳出确认框,缓存服务器不会缓存POST请求返回内容。
安全的是指没有明显的对用户有影响的副作用(包括修改该资源的状态)。HTTP方法里的GET和HEAD都是安全的。
幂等的是指一个方法不论多少次操作,结果都是一样。PUT(把内容放到指定URL),DELETE(删除某个URL代表的资源),虽然都修改了资源内容,但多次操作,结果是相同的,因此和HEAD,GET一样都是幂等的。
所以根据HTTP协议,GET是安全的,也是幂等的,而POST既不是安全的,也不是幂等的。
按约定我们使用Get来做读操作,使用Post来新增或者修改(资源)。
HTTP的其他请求方式
Method | Description |
---|---|
HEAD | Same as GET but returns only HTTP headers and no document body |
PUT | Uploads a representation of the specified URI |
DELETE | Deletes the specified resource |
OPTIONS | Returns the HTTP methods that the server supports |
CONNECT | Converts the request connection to a transparent TCP/IP tunnel |
参考
maximum length of HTTP GET request?
HTTP Method 之 Post VS. Get的更多相关文章
- jsp中出现onclick函数提示Cannot return from outside a function or method
在使用Myeclipse10部署完项目后,原先不出错的项目,会有红色的叉叉,JSP页面会提示onclick函数错误 Cannot return from outside a function or m ...
- Apply Newton Method to Find Extrema in OPEN CASCADE
Apply Newton Method to Find Extrema in OPEN CASCADE eryar@163.com Abstract. In calculus, Newton’s me ...
- 设计模式(九): 从醋溜土豆丝和清炒苦瓜中来学习"模板方法模式"(Template Method Pattern)
今天是五.四青年节,祝大家节日快乐.看着今天这标题就有食欲,夏天到了,醋溜土豆丝和清炒苦瓜适合夏天吃,好吃不上火.这两道菜大部分人都应该吃过,特别是醋溜土豆丝,作为“鲁菜”的代表作之一更是为大众所熟知 ...
- HTTP Method详细解读(`GET` `HEAD` `POST` `OPTIONS` `PUT` `DELETE` `TRACE` `CONNECT`)
前言 HTTP Method的历史: HTTP 0.9 这个版本只有GET方法 HTTP 1.0 这个版本有GET HEAD POST这三个方法 HTTP 1.1 这个版本是当前版本,包含GET HE ...
- IIS7.5上的REST服务的Put,Delete操作发生HTTP Error 405.0 - Method Not Allowed 解决方法
WebDAV 是超文本传输协议 (HTTP) 的一组扩展,为 Internet 上计算机之间的编辑和文件管理提供了标准.利用这个协议用户可以通过Web进行远程的基本文件操作,如拷贝.移动.删除等.在I ...
- The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory
The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory 这是由于项目里面的一些 ...
- Spring boot: Request method 'DELETE' not supported, Request method 'PUT' not supported, Request method 'POST' not supported
GET,POST,PUT,DELETE, Spring都支持,不要怀疑Spring, 一定是前端发送的rest 请求和后端的响应不匹配, 查找原因以及解决办法, 很简单 用chrome打开F12控制台 ...
- 异常:java.lang.LinkageError: loader constraint violation: when resolving interface method
异常:java.lang.LinkageError: loader constraint violation: when resolving interface method "javax. ...
- Error invoking SqlProvider method (com.github.abel533.mapper.MapperProvider.dynamicSQL). Cause: java.lang.InstantiationException: com.github.abel533.mapper.MapperProvider
org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: org.apache.i ...
随机推荐
- DBUtil数据库连接单例 —— 简单不简单
单例大概是我最早产生明确模式意识的设计模式,因为它足够简单粗暴,目的足够明确. 单例么,就是不管怎么访问,都返回一个单一实例就好了,我最早应用在数据库的DBUtil中. public class DB ...
- 开发node桌面级应用工具:apk转化epub
随着苹果ibooks对国内的开放,最近接了个麻烦的需求: 把现有的APK转化支持苹果ibooks电子书的epub格式 apk,基本都知道就是安卓的应用程序 epub,是ibooks支持的电子书格式 ( ...
- 页面width与height使用百分比来划分不起作用解决办法--记录六
有时候你写 <div style="width:80%;height:100%;border:1px solid red"></div> <div s ...
- <a> href属性--记录八
1.去掉<a>标签的下划线 <ul style=" list-style-type:none; margin:0;color:Gray; font-size:11px;ma ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(44)-工作流设计-设计表单
系列目录 设计表单是比较复杂的一步,完成一个表单的设计其实很漫长,主要分为四步. 开始之前先说说表的结构. 其实表Flow_Form与Flow_FormContent设计是有一个缺陷的.我总共是设置最 ...
- 把PDF的底色改成护眼色,这样读起文章来就不是很累了······
PDF格式背景改变方法如下: 打开PDF 点击 编辑 ->首选项->辅助工具->选中"替换文档颜色"和" 自定义颜色"->将背景颜色改成 ...
- 关于Java语言中那些修饰符
一.在java中提供的一些修饰符,这些修饰符可以修饰类.变量和方法,在java中常见的修饰符有:abstract(抽象的).static(静态的).public(公共的).protected(受保护的 ...
- ionic2+angular2中踩的那些坑
好久没写什么东西了,最近在做一个ionic2的小东西,遇到了不少问题,也记录一下,避免后来的同学走弯路. 之前写过一篇使用VS2015开发ionic1的文章,但自己还没摸清门道,本来也是感兴趣就学习了 ...
- EF 在controller弹出提示消息
第一种方式: return Content("<script>alert('此名称课程再次班级中已经存在!');window.location.href = 'Course/Cr ...
- [Tool] Open Live Writer 插件更新
最新插件下载地址:Memento.OLW_V1.0.0.2.7z 零.历史更新记录 2016.11.24 1. 修正 cnblog 语法高亮中的 SQL.Perl 语法高亮异常 下载地址:Mement ...