- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我已经阅读了 HTTP 文档,但我无法理解什么是幂等性。有人可以帮忙吗?
最佳答案
What is idempotency in HTTP methods?
幂等性 是 HTTP 方法的一个属性。
如果使用该方法的多个相同请求在服务器上的预期效果与效果相同,则该请求方法被视为幂等对于单个这样的请求。值得一提的是,幂等性是关于服务器上资源状态产生的效果,而不是关于收到的响应状态代码由客户。
为了说明这一点,考虑定义为幂等的 DELETE
方法。现在考虑客户端执行 DELETE
请求以从服务器删除资源。服务器处理请求,资源被删除,服务器返回 204
。然后客户端重复相同的 DELETE
请求,由于资源已被删除,服务器返回 404
。
尽管客户端收到的状态代码不同,但单个 DELETE
请求产生的效果与对同一 URI 的多个 DELETE
请求产生的效果相同。
最后,如果在客户端能够读取服务器响应之前发生通信故障,则可以自动重复使用幂等方法的请求。客户端知道重复请求将具有相同的预期效果,即使原始请求成功,尽管响应可能不同。
让我们看看 RFC 7231 ,文档定义了 HTTP/1.1 协议(protocol)的语义和内容。请参阅下面的引用(重点是我的)。
HTTP 方法可以安全:
Request methods are considered "safe" if their defined semantics are essentially read-only; i.e., the client does not request, and does not expect, any state change on the origin server as a result of applying a safe method to a target resource. [...]
This definition of safe methods does not prevent an implementation from including behavior that is potentially harmful, that is not entirely read-only, or that causes side effects while invoking a safe method. What is important, however, is that the client did not request that additional behavior and cannot be held accountable for it. [...]
Of the request methods defined by this specification, the
GET
,HEAD
,OPTIONS
, andTRACE
methods are defined to be safe. [...]
和/或幂等:
A request method is considered "idempotent" if the intended effect on the server of multiple identical requests with that method is the same as the effect for a single such request. Of the request methods defined by this specification,
PUT
,DELETE
, and safe request methods are idempotent. [...]Like the definition of safe, the idempotent property only applies to what has been requested by the user; a server is free to log each request separately, retain a revision control history, or implement other non-idempotent side effects for each idempotent request. [...]
总而言之,HTTP 方法被分类为 following :
+---------+------+------------+
| Method | Safe | Idempotent |
+---------+------+------------+
| CONNECT | no | no |
| DELETE | no | yes |
| GET | yes | yes |
| HEAD | yes | yes |
| OPTIONS | yes | yes |
| POST | no | no |
| PUT | no | yes |
| TRACE | yes | yes |
+---------+------+------------+
RFC 5789定义 PATCH
方法,既不安全也不幂等。但是,为了防止碰撞,PATCH
可以以幂等的方式发出请求,如下所述:
A
PATCH
request can be issued in such a way as to be idempotent, which also helps prevent bad outcomes from collisions between twoPATCH
requests on the same resource in a similar time frame. Collisions from multiplePATCH
requests may be more dangerous thanPUT
collisions because some patch formats need to operate from a known base-point or else they will corrupt the resource. Clients using this kind of patch application SHOULD use a conditional request such that the request will fail if the resource has been updated since the client last accessed the resource. For example, the client can use a strongETag
in anIf-Match
header on thePATCH
request.
关于http - 什么是 HTTP 方法中的幂等性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45016234/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!