1. 程式人生 > >區別URI和URL

區別URI和URL

trac 命名 specified https rep depend top solution class

  本人在看《圖解HTTP》的時候,剛開始還對URI和URL的區別不是太在意,越到後面越發覺區別兩者的必要性。

  英文文檔:“A URI is a uniform resource identifier while a URL is a uniform resource locator. Hence every URL is a URI, abstractly speaking, but not every URI is a URL. This is because there is another subcategory of URIs, uniform resource names (URNs), which name resources but do not specify how to locate them. The mailto, news, and isbn URIs shown above are examples of URNs.

The conceptual distinction between URIs and URLs is reflected in the differences between this class and the URL class.

An instance of this class represents a URI reference in the syntactic sense defined by RFC 2396. A URI may be either absolute or relative. A URI string is parsed according to the generic syntax without regard to the scheme, if any, that it specifies. No lookup of the host, if any, is performed, and no scheme-dependent stream handler is constructed. Equality, hashing, and comparison are defined strictly in terms of the character content of the instance. In other words, a URI instance is little more than a structured string that supports the syntactic, scheme-independent operations of comparison, normalization, resolution, and relativization.

An instance of the URL class, by contrast, represents the syntactic components of a URL together with some of the information required to access the resource that it describes. A URL must be absolute, that is, it must always specify a scheme. A URL string is parsed according to its scheme. A stream handler is always established for a URL, and in fact it is impossible to create a URL instance for a scheme for which no handler is available. Equality and hashing depend upon both the scheme and the Internet address of the host, if any; comparison is not defined. In other words, a URL is a structured string that supports the syntactic operation of resolution as well as the network I/O operations of looking up the host and opening a connection to the specified resource.”

  中文文檔:"URI 是統一資源標識符,而 URL 是統一資源定位符。因此,籠統地說,每個 URL 都是 URI,但不一定每個 URI 都是 URL。這是因為 URI 還包括一個子類,即統一資源名稱 (URN),它命名資源但不指定如何定位資源。上面的 mailtonewsisbn URI 都是 URN 的示例。

URI 和 URL 概念上的不同反映在此類和 URL 類的不同中。

此類的實例代表由 RFC 2396 定義的語法意義上的一個 URI 引用。URI 可以是絕對的,也可以是相對的。對 URI 字符串按照一般語法進行解析,不考慮它所指定的方案(如果有)不對主機(如果有)執行查找,也不構造依賴於方案的流處理程序。相等性、哈希計算以及比較都嚴格地根據實例的字符內容進行定義。換句話說,一個 URI 實例和一個支持語法意義上的、依賴於方案的比較、規範化、解析和相對化計算的結構化字符串差不多。

作為對照,URL 類的實例代表了 URL 的語法組成部分以及訪問它描述的資源所需的信息。URL 必須是絕對的,即它必須始終指定一個方案。URL 字符串按照其方案進行解析。通常會為 URL 建立一個流處理程序,實際上無法為未提供處理程序的方案創建一個 URL 實例。相等性和哈希計算依賴於方案和主機的 Internet 地址(如果有);沒有定義比較。換句話說,URL 是一個結構化字符串,它支持解析的語法運算以及查找主機和打開到指定資源的連接之類的網絡 I/O 操作。

"

  以上是從官方文檔對於URI的解釋中出現的,對於URI、URL、URN的區別。簡單地看,可以用一個表格來說明這三者的區別。

名詞

特性

URI統一資源標識符

既可以相對地也可以絕對地標識一個資源的地址

URL統一資源定位符

是比URI更具體形式,只能是絕對地標識一個資源的地址

URN統一資源名稱

只命名一個資源但不指定如何定位資源

  

  參考網址:https://docs.oracle.com/javase/8/docs/api/java/net/URI.html

       http://download.oracle.com/technetwork/java/javase/6/docs/zh/api/java/net/URI.html

區別URI和URL