Condividi tramite


URI Class

Definition

Represents a Uniform Resource Identifier (URI) reference.

[Android.Runtime.Register("java/net/URI", DoNotGenerateAcw=true)]
public sealed class URI : Java.Lang.Object, IDisposable, Java.Interop.IJavaPeerable, Java.IO.ISerializable, Java.Lang.IComparable
[<Android.Runtime.Register("java/net/URI", DoNotGenerateAcw=true)>]
type URI = class
    inherit Object
    interface ISerializable
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
    interface IComparable
Inheritance
Attributes
Implements

Remarks

Represents a Uniform Resource Identifier (URI) reference.

Aside from some minor deviations noted below, an instance of this class represents a URI reference as defined by RFC&nbsp;2396: Uniform Resource Identifiers (URI): Generic Syntaxhttp://www.ietf.org/rfc/rfc2396.txt, amended by RFC&nbsp;2732: Format for Literal IPv6 Addresses in URLshttp://www.ietf.org/rfc/rfc2732.txt. The Literal IPv6 address format also supports scope_ids. The syntax and usage of scope_ids is described here. This class provides constructors for creating URI instances from their components or by parsing their string forms, methods for accessing the various components of an instance, and methods for normalizing, resolving, and relativizing URI instances. Instances of this class are immutable.

<h3> URI syntax and components </h3>

At the highest level a URI reference (hereinafter simply "URI") in string form has the syntax

<blockquote> [scheme<b>:</b>]scheme-specific-part[<b>#</b>fragment] </blockquote>

where square brackets [...] delineate optional components and the characters <b>:</b> and <b>#</b> stand for themselves.

An absolute URI specifies a scheme; a URI that is not absolute is said to be relative. URIs are also classified according to whether they are opaque or hierarchical.

An opaque URI is an absolute URI whose scheme-specific part does not begin with a slash character ('/'). Opaque URIs are not subject to further parsing. Some examples of opaque URIs are:

<blockquote><table cellpadding=0 cellspacing=0 summary="layout"> <tr><td>mailto:java-net@java.sun.com<td></tr> <tr><td>news:comp.lang.java<td></tr> <tr><td>urn:isbn:096139210x</td></tr> </table></blockquote>

A hierarchical URI is either an absolute URI whose scheme-specific part begins with a slash character, or a relative URI, that is, a URI that does not specify a scheme. Some examples of hierarchical URIs are:

<blockquote> http://java.sun.com/j2se/1.3/<br> docs/guide/collections/designfaq.html#28<br> ../../../demo/jfc/SwingSet2/src/SwingSet2.java<br> file:///~/calendar</blockquote>

A hierarchical URI is subject to further parsing according to the syntax

<blockquote> [scheme<b>:</b>][<b>//</b>authority][path][<b>?</b>query][<b>#</b>fragment] </blockquote>

where the characters <b>:</b>, <b>/</b>, <b>?</b>, and <b>#</b> stand for themselves. The scheme-specific part of a hierarchical URI consists of the characters between the scheme and fragment components.

The authority component of a hierarchical URI is, if specified, either server-based or registry-based. A server-based authority parses according to the familiar syntax

<blockquote> [user-info<b>@</b>]host[<b>:</b>port] </blockquote>

where the characters <b>@</b> and <b>:</b> stand for themselves. Nearly all URI schemes currently in use are server-based. An authority component that does not parse in this way is considered to be registry-based.

The path component of a hierarchical URI is itself said to be absolute if it begins with a slash character ('/'); otherwise it is relative. The path of a hierarchical URI that is either absolute or specifies an authority is always absolute.

All told, then, a URI instance has the following nine components:

<blockquote><table summary="Describes the components of a URI:scheme,scheme-specific-part,authority,user-info,host,port,path,query,fragment"> <tr><th>Component</th><th>Type</th></tr> <tr><td>scheme</td><td>String</td></tr> <tr><td>scheme-specific-part&nbsp;&nbsp;&nbsp;&nbsp;</td><td>String</td></tr> <tr><td>authority</td><td>String</td></tr> <tr><td>user-info</td><td>String</td></tr> <tr><td>host</td><td>String</td></tr> <tr><td>port</td><td>int</td></tr> <tr><td>path</td><td>String</td></tr> <tr><td>query</td><td>String</td></tr> <tr><td>fragment</td><td>String</td></tr> </table></blockquote>

In a given instance any particular component is either undefined or defined with a distinct value. Undefined string components are represented by null, while undefined integer components are represented by -1. A string component may be defined to have the empty string as its value; this is not equivalent to that component being undefined.

Whether a particular component is or is not defined in an instance depends upon the type of the URI being represented. An absolute URI has a scheme component. An opaque URI has a scheme, a scheme-specific part, and possibly a fragment, but has no other components. A hierarchical URI always has a path (though it may be empty) and a scheme-specific-part (which at least contains the path), and may have any of the other components. If the authority component is present and is server-based then the host component will be defined and the user-information and port components may be defined.

<h4> Operations on URI instances </h4>

The key operations supported by this class are those of normalization, resolution, and relativization.

Normalization is the process of removing unnecessary "." and ".." segments from the path component of a hierarchical URI. Each "." segment is simply removed. A ".." segment is removed only if it is preceded by a non-".." segment. Normalization has no effect upon opaque URIs.

Resolution is the process of resolving one URI against another, base URI. The resulting URI is constructed from components of both URIs in the manner specified by RFC&nbsp;2396, taking components from the base URI for those not specified in the original. For hierarchical URIs, the path of the original is resolved against the path of the base and then normalized. The result, for example, of resolving

<blockquote> docs/guide/collections/designfaq.html#28 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;(1) </blockquote>

against the base URI http://java.sun.com/j2se/1.3/ is the result URI

<blockquote> http://java.sun.com/j2se/1.3/docs/guide/collections/designfaq.html#28</blockquote>

Resolving the relative URI

<blockquote> ../../../demo/jfc/SwingSet2/src/SwingSet2.java&nbsp;&nbsp;&nbsp;&nbsp;(2) </blockquote>

against this result yields, in turn,

<blockquote> http://java.sun.com/j2se/1.3/demo/jfc/SwingSet2/src/SwingSet2.java</blockquote>

Resolution of both absolute and relative URIs, and of both absolute and relative paths in the case of hierarchical URIs, is supported. Resolving the URI file:///~calendar against any other URI simply yields the original URI, since it is absolute. Resolving the relative URI (2) above against the relative base URI (1) yields the normalized, but still relative, URI

<blockquote> demo/jfc/SwingSet2/src/SwingSet2.java</blockquote>

Relativization, finally, is the inverse of resolution: For any two normalized URIs u and&nbsp;v,

<blockquote> u.relativize(u.resolve(v)).equals(v)&nbsp;&nbsp;and<br> u.resolve(u.relativize(v)).equals(v)&nbsp;&nbsp;.<br> </blockquote>

This operation is often useful when constructing a document containing URIs that must be made relative to the base URI of the document wherever possible. For example, relativizing the URI

<blockquote> http://java.sun.com/j2se/1.3/docs/guide/index.html</blockquote>

against the base URI

<blockquote> http://java.sun.com/j2se/1.3</blockquote>

yields the relative URI docs/guide/index.html.

<h4> Character categories </h4>

RFC&nbsp;2396 specifies precisely which characters are permitted in the various components of a URI reference. The following categories, most of which are taken from that specification, are used below to describe these constraints:

<blockquote><table cellspacing=2 summary="Describes categories alpha,digit,alphanum,unreserved,punct,reserved,escaped,and other"> <tr><th valign=top>alpha</th> <td>The US-ASCII alphabetic characters, 'A'&nbsp;through&nbsp;'Z' and 'a'&nbsp;through&nbsp;'z'</td></tr> <tr><th valign=top>digit</th> <td>The US-ASCII decimal digit characters, '0'&nbsp;through&nbsp;'9'</td></tr> <tr><th valign=top>alphanum</th> <td>All alpha and digit characters</td></tr> <tr><th valign=top>unreserved&nbsp;&nbsp;&nbsp;&nbsp;</th> <td>All alphanum characters together with those in the string "_-!.~'()*"</td></tr> <tr><th valign=top>punct</th> <td>The characters in the string ",;:$&+="</td></tr> <tr><th valign=top>reserved</th> <td>All punct characters together with those in the string "?/[]@"</td></tr> <tr><th valign=top>escaped</th> <td>Escaped octets, that is, triplets consisting of the percent character ('%') followed by two hexadecimal digits ('0'-'9', 'A'-'F', and 'a'-'f')</td></tr> <tr><th valign=top>other</th> <td>The Unicode characters that are not in the US-ASCII character set, are not control characters (according to the java.lang.Character#isISOControl(char) Character.isISOControl method), and are not space characters (according to the java.lang.Character#isSpaceChar(char) Character.isSpaceChar method)&nbsp;&nbsp;(<b>Deviation from RFC 2396</b>, which is limited to US-ASCII)</td></tr> </table></blockquote>

"legal-chars"> The set of all legal URI characters consists of the unreserved, reserved, escaped, and other characters.

<h4> Escaped octets, quotation, encoding, and decoding </h4>

RFC 2396 allows escaped octets to appear in the user-info, path, query, and fragment components. Escaping serves two purposes in URIs:

<ul>

<li>

To encode non-US-ASCII characters when a URI is required to conform strictly to RFC&nbsp;2396 by not containing any other characters.

</li>

<li>

To quote characters that are otherwise illegal in a component. The user-info, path, query, and fragment components differ slightly in terms of which characters are considered legal and illegal.

</li>

</ul>

These purposes are served in this class by three related operations:

<ul>

<li>

"encode"> A character is encoded by replacing it with the sequence of escaped octets that represent that character in the UTF-8 character set. The Euro currency symbol ('\u005Cu20AC'), for example, is encoded as "%E2%82%AC". (<b>Deviation from RFC&nbsp;2396</b>, which does not specify any particular character set.)

</li>

<li>

"quote"> An illegal character is quoted simply by encoding it. The space character, for example, is quoted by replacing it with "%20". UTF-8 contains US-ASCII, hence for US-ASCII characters this transformation has exactly the effect required by RFC&nbsp;2396.

</li>

<li>

"decode"> A sequence of escaped octets is decoded by replacing it with the sequence of characters that it represents in the UTF-8 character set. UTF-8 contains US-ASCII, hence decoding has the effect of de-quoting any quoted US-ASCII characters as well as that of decoding any encoded non-US-ASCII characters. If a decoding error occurs when decoding the escaped octets then the erroneous octets are replaced by '\u005CuFFFD', the Unicode replacement character.

</li>

</ul>

These operations are exposed in the constructors and methods of this class as follows:

<ul>

<li>

The #URI(java.lang.String) single-argument constructor requires any illegal characters in its argument to be quoted and preserves any escaped octets and other characters that are present.

</li>

<li>

The #URI(java.lang.String,java.lang.String,java.lang.String,int,java.lang.String,java.lang.String,java.lang.String) multi-argument constructors quote illegal characters as required by the components in which they appear. The percent character ('%') is always quoted by these constructors. Any other characters are preserved.

</li>

<li>

The #getRawUserInfo() getRawUserInfo, #getRawPath() getRawPath, #getRawQuery() getRawQuery, #getRawFragment() getRawFragment, #getRawAuthority() getRawAuthority, and #getRawSchemeSpecificPart() getRawSchemeSpecificPart methods return the values of their corresponding components in raw form, without interpreting any escaped octets. The strings returned by these methods may contain both escaped octets and other characters, and will not contain any illegal characters.

</li>

<li>

The #getUserInfo() getUserInfo, #getPath() getPath, #getQuery() getQuery, #getFragment() getFragment, #getAuthority() getAuthority, and #getSchemeSpecificPart() getSchemeSpecificPart methods decode any escaped octets in their corresponding components. The strings returned by these methods may contain both other characters and illegal characters, and will not contain any escaped octets.

</li>

<li>

The #toString() toString method returns a URI string with all necessary quotation but which may contain other characters.

</li>

<li>

The #toASCIIString() toASCIIString method returns a fully quoted and encoded URI string that does not contain any other characters.

</li>

</ul>

<h4> Identities </h4>

For any URI u, it is always the case that

<blockquote> new URI(u.toString()).equals(u)&nbsp;. </blockquote>

For any URI u that does not contain redundant syntax such as two slashes before an empty authority (as in file:///tmp/&nbsp;) or a colon following a host name but no port (as in http://java.sun.com:&nbsp;), and that does not encode characters except those that must be quoted, the following identities also hold:

new URI(<i>u</i>.getScheme(),
<i>u</i>.getSchemeSpecificPart(),
<i>u</i>.getFragment())
                .equals(<i>u</i>)

in all cases,

new URI(<i>u</i>.getScheme(),
<i>u</i>.getUserInfo(), <i>u</i>.getAuthority(),
<i>u</i>.getPath(), <i>u</i>.getQuery(),
<i>u</i>.getFragment())
                .equals(<i>u</i>)

if u is hierarchical, and

new URI(<i>u</i>.getScheme(),
<i>u</i>.getUserInfo(), <i>u</i>.getHost(), <i>u</i>.getPort(),
<i>u</i>.getPath(), <i>u</i>.getQuery(),
<i>u</i>.getFragment())
                .equals(<i>u</i>)

if u is hierarchical and has either no authority or a server-based authority.

<h4> URIs, URLs, and URNs </h4>

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&nbsp;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.

Added in 1.4.

Java documentation for java.net.URI.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

URI(String, String, String, Int32, String, String, String)

Constructs a hierarchical URI from the given components.

URI(String, String, String, String, String)

Constructs a hierarchical URI from the given components.

URI(String, String, String, String)

Constructs a hierarchical URI from the given components.

URI(String, String, String)

Constructs a URI from the given components.

URI(String)

Constructs a URI by parsing the given string.

Properties

Authority

Returns the decoded authority component of this URI.

Class

Returns the runtime class of this Object.

(Inherited from Object)
Fragment

Returns the decoded fragment component of this URI.

Handle

The handle to the underlying Android instance.

(Inherited from Object)
Host

Returns the host component of this URI.

IsAbsolute

Tells whether or not this URI is absolute.

IsOpaque

Tells whether or not this URI is opaque.

JniIdentityHashCode (Inherited from Object)
JniPeerMembers
Path

Returns the decoded path component of this URI.

PeerReference (Inherited from Object)
Port

Returns the port number of this URI.

Query

Returns the decoded query component of this URI.

RawAuthority

Returns the raw authority component of this URI.

RawFragment

Returns the raw fragment component of this URI.

RawPath

Returns the raw path component of this URI.

RawQuery

Returns the raw query component of this URI.

RawSchemeSpecificPart

Returns the raw scheme-specific part of this URI.

RawUserInfo

Returns the raw user-information component of this URI.

Scheme

Returns the scheme component of this URI.

SchemeSpecificPart

Returns the decoded scheme-specific part of this URI.

ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

(Inherited from Object)
ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

(Inherited from Object)
UserInfo

Returns the decoded user-information component of this URI.

Methods

Clone()

Creates and returns a copy of this object.

(Inherited from Object)
CompareTo(URI)

Compares this URI to another object, which must be a URI.

Create(String)

Creates a URI by parsing the given string.

Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
Normalize()

Normalizes this URI's path.

Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
ParseServerAuthority()

Attempts to parse this URI's authority component, if defined, into user-information, host, and port components.

Relativize(URI)

Relativizes the given URI against this URI.

Resolve(String)

Constructs a new URI by parsing the given string and then resolving it against this URI.

Resolve(URI)

Resolves the given URI against this URI.

SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
ToArray<T>() (Inherited from Object)
ToASCIIString()

Returns the content of this URI as a US-ASCII string.

ToString()

Returns a string representation of the object.

(Inherited from Object)
ToURL()

Constructs a URL from this URI.

UnregisterFromRuntime() (Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)

Explicit Interface Implementations

IComparable.CompareTo(Object)
IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to