Skip to content

Most visited

Recently visited

navigation
Added in API level 1

URL

public final class URL
extends Object implements Serializable

java.lang.Object
   ↳ java.net.URL


A Uniform Resource Locator that identifies the location of an Internet resource as specified by RFC 1738.

Parts of a URL

A URL is composed of many parts. This class can both parse URL strings into parts and compose URL strings from parts. For example, consider the parts of this URL: http://username:password@host:8080/directory/file?query#ref:
ComponentExample valueAlso known as
Protocolhttpscheme
Authorityusername:password@host:8080
User Infousername:password
Hosthost
Port8080
File/directory/file?query
Path/directory/file
Queryquery
Refreffragment

Supported Protocols

This class may be used to construct URLs with the following protocols: In general, attempts to create URLs with any other protocol will fail with a MalformedURLException. Applications may install handlers for other schemes using setURLStreamHandlerFactory(URLStreamHandlerFactory) or with the java.protocol.handler.pkgs system property.

The URI class can be used to manipulate URLs of any protocol.

Summary

Public constructors

URL(String spec)

Creates a new URL instance by parsing spec.

URL(URL context, String spec)

Creates a new URL by resolving spec relative to context.

URL(URL context, String spec, URLStreamHandler handler)

Creates a new URL by resolving spec relative to context.

URL(String protocol, String host, String file)

Creates a new URL of the given component parts.

URL(String protocol, String host, int port, String file)

Creates a new URL of the given component parts.

URL(String protocol, String host, int port, String file, URLStreamHandler handler)

Creates a new URL of the given component parts.

Public methods

boolean equals(Object o)

Returns true if this URL equals o.

String getAuthority()

Returns the authority part of this URL, or null if this URL has no authority.

final Object getContent(Class[] types)

Equivalent to openConnection().getContent(types).

final Object getContent()

Returns the content of the resource which is referred by this URL.

int getDefaultPort()

Returns the default port number of the protocol used by this URL.

String getFile()

Returns the file of this URL.

String getHost()

Returns the host name or IP address of this URL.

String getPath()

Returns the path part of this URL.

int getPort()

Returns the port number of this URL or -1 if this URL has no explicit port.

String getProtocol()

Returns the protocol of this URL like "http" or "file".

String getQuery()

Returns the query part of this URL, or null if this URL has no query.

String getRef()

Returns the value of the reference part of this URL, or null if this URL has no reference part.

String getUserInfo()

Returns the user info of this URL, or null if this URL has no user info.

int hashCode()

Returns an integer hash code for this object.

URLConnection openConnection(Proxy proxy)

Returns a new connection to the resource referred to by this URL.

URLConnection openConnection()

Returns a new connection to the resource referred to by this URL.

final InputStream openStream()

Equivalent to openConnection().getInputStream(types).

boolean sameFile(URL otherURL)

Returns true if this URL refers to the same resource as otherURL.

static void setURLStreamHandlerFactory(URLStreamHandlerFactory factory)

Sets the stream handler factory for this VM.

String toExternalForm()

Returns a string containing a concise, human-readable representation of this URL.

String toString()

Returns a string containing a concise, human-readable representation of this URL.

URI toURI()

Returns the URI equivalent to this URL.

Protected methods

void set(String protocol, String host, int port, String authority, String userInfo, String path, String query, String ref)

Sets the properties of this URL using the provided arguments.

void set(String protocol, String host, int port, String file, String ref)

Sets the properties of this URL using the provided arguments.

Inherited methods

From class java.lang.Object

Public constructors

URL

Added in API level 1
URL (String spec)

Creates a new URL instance by parsing spec.

Parameters
spec String
Throws
MalformedURLException if spec could not be parsed as a URL.

URL

Added in API level 1
URL (URL context, 
                String spec)

Creates a new URL by resolving spec relative to context.

Parameters
context URL: the URL to which spec is relative, or null for no context in which case spec must be an absolute URL.
spec String
Throws
MalformedURLException if spec could not be parsed as a URL or has an unsupported protocol.

URL

Added in API level 1
URL (URL context, 
                String spec, 
                URLStreamHandler handler)

Creates a new URL by resolving spec relative to context.

Parameters
context URL: the URL to which spec is relative, or null for no context in which case spec must be an absolute URL.
spec String
handler URLStreamHandler: the stream handler for this URL, or null for the protocol's default stream handler.
Throws
MalformedURLException if the given string spec could not be parsed as a URL or an invalid protocol has been found.

URL

Added in API level 1
URL (String protocol, 
                String host, 
                String file)

Creates a new URL of the given component parts. The URL uses the protocol's default port.

Parameters
protocol String
host String
file String
Throws
MalformedURLException if the combination of all arguments do not represent a valid URL or if the protocol is invalid.

URL

Added in API level 1
URL (String protocol, 
                String host, 
                int port, 
                String file)

Creates a new URL of the given component parts. The URL uses the protocol's default port.

Parameters
protocol String
host String: the host name or IP address of the new URL.
port int: the port, or -1 for the protocol's default port.
file String: the name of the resource.
Throws
MalformedURLException if the combination of all arguments do not represent a valid URL or if the protocol is invalid.

URL

Added in API level 1
URL (String protocol, 
                String host, 
                int port, 
                String file, 
                URLStreamHandler handler)

Creates a new URL of the given component parts. The URL uses the protocol's default port.

Parameters
protocol String
host String: the host name or IP address of the new URL.
port int: the port, or -1 for the protocol's default port.
file String: the name of the resource.
handler URLStreamHandler: the stream handler for this URL, or null for the protocol's default stream handler.
Throws
MalformedURLException if the combination of all arguments do not represent a valid URL or if the protocol is invalid.

Public methods

equals

Added in API level 1
boolean equals (Object o)

Returns true if this URL equals o. URLs are equal if they have the same protocol, host, port, file, and reference.

Network I/O Warning

Some implementations of URL.equals() resolve host names over the network. This is problematic:

  • The network may be slow. Many classes, including core collections like Map and Set expect that equals and hashCode will return quickly. By violating this assumption, this method posed potential performance problems.
  • Equal IP addresses do not imply equal content. Virtual hosting permits unrelated sites to share an IP address. This method could report two otherwise unrelated URLs to be equal because they're hosted on the same server.
  • The network may not be available. Two URLs could be equal when a network is available and unequal otherwise.
  • The network may change. The IP address for a given host name varies by network and over time. This is problematic for mobile devices. Two URLs could be equal on some networks and unequal on others.

This problem is fixed in Android 4.0 (Ice Cream Sandwich). In that release, URLs are only equal if their host names are equal (ignoring case).

Parameters
o Object: the object to compare this instance with.
Returns
boolean true if the specified object is equal to this Object; false otherwise.

getAuthority

Added in API level 1
String getAuthority ()

Returns the authority part of this URL, or null if this URL has no authority.

Returns
String

getContent

Added in API level 1
Object getContent (Class[] types)

Equivalent to openConnection().getContent(types).

Parameters
types Class
Returns
Object
Throws
IOException

getContent

Added in API level 1
Object getContent ()

Returns the content of the resource which is referred by this URL. By default this returns an InputStream, or null if the content type of the response is unknown.

Returns
Object
Throws
IOException

getDefaultPort

Added in API level 1
int getDefaultPort ()

Returns the default port number of the protocol used by this URL. If no default port is defined by the protocol or the URLStreamHandler, -1 will be returned.

Returns
int

See also:

getFile

Added in API level 1
String getFile ()

Returns the file of this URL.

Returns
String

getHost

Added in API level 1
String getHost ()

Returns the host name or IP address of this URL.

Returns
String

getPath

Added in API level 1
String getPath ()

Returns the path part of this URL.

Returns
String

getPort

Added in API level 1
int getPort ()

Returns the port number of this URL or -1 if this URL has no explicit port.

If this URL has no explicit port, connections opened using this URL will use its default port.

Returns
int

getProtocol

Added in API level 1
String getProtocol ()

Returns the protocol of this URL like "http" or "file". This is also known as the scheme. The returned string is lower case.

Returns
String

getQuery

Added in API level 1
String getQuery ()

Returns the query part of this URL, or null if this URL has no query.

Returns
String

getRef

Added in API level 1
String getRef ()

Returns the value of the reference part of this URL, or null if this URL has no reference part. This is also known as the fragment.

Returns
String

getUserInfo

Added in API level 1
String getUserInfo ()

Returns the user info of this URL, or null if this URL has no user info.

Returns
String

hashCode

Added in API level 1
int hashCode ()

Returns an integer hash code for this object. By contract, any two objects for which equals(Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

Note that hash values must not change over time unless information used in equals comparisons also changes.

See Writing a correct hashCode method if you intend implementing your own hashCode method.

Returns
int this object's hash code.

openConnection

Added in API level 1
URLConnection openConnection (Proxy proxy)

Returns a new connection to the resource referred to by this URL.

Parameters
proxy Proxy: the proxy through which the connection will be established.
Returns
URLConnection
Throws
IOException if an I/O error occurs while opening the connection.
IllegalArgumentException if the argument proxy is null or of is an invalid type.
UnsupportedOperationException if the protocol handler does not support opening connections through proxies.

openConnection

Added in API level 1
URLConnection openConnection ()

Returns a new connection to the resource referred to by this URL.

Returns
URLConnection
Throws
IOException if an error occurs while opening the connection.

openStream

Added in API level 1
InputStream openStream ()

Equivalent to openConnection().getInputStream(types).

Returns
InputStream
Throws
IOException

sameFile

Added in API level 1
boolean sameFile (URL otherURL)

Returns true if this URL refers to the same resource as otherURL. All URL components except the reference field are compared.

Parameters
otherURL URL
Returns
boolean

setURLStreamHandlerFactory

Added in API level 1
void setURLStreamHandlerFactory (URLStreamHandlerFactory factory)

Sets the stream handler factory for this VM.

Parameters
factory URLStreamHandlerFactory
Throws
Error if a URLStreamHandlerFactory has already been installed for the current VM.

toExternalForm

Added in API level 1
String toExternalForm ()

Returns a string containing a concise, human-readable representation of this URL.

Returns
String

toString

Added in API level 1
String toString ()

Returns a string containing a concise, human-readable representation of this URL. The returned string is the same as the result of the method toExternalForm().

Returns
String a printable representation of this object.

toURI

Added in API level 1
URI toURI ()

Returns the URI equivalent to this URL.

Returns
URI
Throws
URISyntaxException if this URL cannot be converted into a URI.

Protected methods

set

Added in API level 1
void set (String protocol, 
                String host, 
                int port, 
                String authority, 
                String userInfo, 
                String path, 
                String query, 
                String ref)

Sets the properties of this URL using the provided arguments. Only a URLStreamHandler can use this method to set fields of the existing URL instance. A URL is generally constant.

Parameters
protocol String
host String
port int
authority String
userInfo String
path String
query String
ref String

set

Added in API level 1
void set (String protocol, 
                String host, 
                int port, 
                String file, 
                String ref)

Sets the properties of this URL using the provided arguments. Only a URLStreamHandler can use this method to set fields of the existing URL instance. A URL is generally constant.

Parameters
protocol String
host String
port int
file String
ref String
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.