It's been explained to me numerous times that all URLs are URIs but not all URIs are URLs. Can anyone give an example of something that is a URI but is not a URL?
7 回答
Example stolen from here (where there is also a description of the differences):
URL http://www.pierobon.org/iis/review1.htm
URN www.pierobon.org/iis/review1.htm#one
URI http://www.pierobon.org/iis/review1.htm.html#one
urn:isbn:0451450523
A Uniform Resource Name (URN) is a URI that identifies a resource by name in a particular namespace. A URN can be used to talk about a resource without implying its location or how to access it. For example, the URN urn:isbn:0-395-36341-1 is a URI
From: Wikipedia: http://en.wikipedia.org/wiki/Uniform_Resource_Identifier
XML Schema 通常用 URI 标识,虽然它们的格式可能类似,但不能保证那里有任何东西,因为它不是 URL。
如果您必须验证 XML 文件,则必须能够识别正确的 XML 模式。在成功验证意味着任何有用之前,必须在内容和模式作者之间共享一种标识方法。URI 可以很好地满足这一需求。请记住,使用 XML 文件不一定需要模式。因此,它不需要是通用的可定位或可用的,它只需要是可识别的。URI 的语义避免了资源必须位于“此处”的暗示,就像 URL 的情况一样,并且有充分的理由。这样的细节与识别任务无关。
Schema 发布者通常会基于他们拥有的 URL 建立 URI。我可以想象这样做有很多原因,但其中一个原因是,它有助于避免在没有中介的情况下发生命名冲突。当使用这样的约定时,很难拒绝将定义托管在 URI 指向的位置(如果它位于 URL 的位置)。虽然通过了解手段是必要的,但我相信这样做标志着一项值得赞赏的努力,并且是良好信息架构的一个示例,但这一事实与 URI 满足的需求无关。
既不是 URL 也不是 URN 的 URI 的示例是数据 URI,例如: data:,Hello%20World 它既不是 URL 也不是 URN,因为 URI 包含数据。它既没有命名它,也没有告诉您如何通过网络找到它。
取自这里:https ://webmasters.stackexchange.com/questions/19101/what-is-the-difference-between-a-uri-and-a-url
我相信 TofuBeer 的答案(迄今为止投票最多)是错误的。
A common case would be a URN, which is a URI of the format urn:namespace-id:resource-id
. Per Wikipedia:
Defined in 1997 in RFC 2141, URNs were intended to serve as persistent, location-independent identifiers, allowing the simple mapping of namespaces into a single URN namespace. The existence of such a URI does not imply availability of the identified resource, but such URIs are required to remain globally unique and persistent, even when the resource ceases to exist or becomes unavailable.
Both styles of resource reference (URL and URN) were later combined under the concept of the URI, but they remain recognized as serving distinct purposes (emphasis mine):
A Uniform Resource Name (URN) can be compared to a person's name, while a Uniform Resource Locator (URL) can be compared to their street address. In other words, a URN identifies an item and a URL provides a method for finding it.
为了测试在我的单元测试中失败的异常(涵盖 Java 中的 URI ←→ URL 转换),我使用以下示例:
"_ "
— 无效URI
自String
"data://valid-uri"
— 有效URI
,但无效,格式错误URL
(MalformedURLException
从 抛出URI#toURL
)。"valid-uri"
— 有效URI
,但无效,非绝对URL
(IllegalArgumentException
从 抛出URI#toURL
)。"http:// _"
— 有效URL
,但无效,格式错误URI
(URISyntaxException
从 抛出URL#toURI
)。
它可能不是 RFC 规范的完美答案,但这些是 Java 的工作示例。