Java Reference
In-Depth Information
line 521111
line 521112
Communicating via URLs
A
Uniform Resource Locator (URL)
isacharacterstringthatspecifieswherearesource
(e.g., a web page) is located on a TCP/IP-based network (e.g., the Internet). Also,
it provides the means to retrieve that resource. For example,
ht-
tp://
prefixspecifiesthat
HyperText Transfer Protocol (HTTP)
,whichisahigh-level
protocolontopofTCP/IPforlocatingHTTPresources(e.g.,webpages),mustbeused
to retrieve the web page located at
tutortutor.ca
.
URNS AND URIS
A
Uniform Resource Name (URN)
isacharacterstringthatdoesn'timplyaresource's
availability. Even when the resource is available, the URN doesn't provide a way
tolocateit.Forexample,
urn:isbn:9781430234135
identifiesanApressbook
named
Android Recipes
, and that's all.
URNs and URLs are examples of
Uniform Resource Identifiers (URIs)
, which are
characterstringsforidentifyingnames(URNs)orresources(URLs).EveryURNand
URLisalsoaURI,afactthatItakeadvantageofinsubsequentchaptersbyspecify-
ing URI instead of URL.
The
java.net
packageprovides
URL
and
URLConnection
classesforaccessing
URL-based resources. It also provides
URLEncoder
and
URLDecoder
classes for
encodinganddecodingURLs,andthe
URI
classforperformingURI-basedoperations
(e.g., relativization) and returning
URL
instances containing the results.
URL and URLConnection
The
URL
classrepresentsURLsandprovidesaccesstotheresourcestowhichtheyrefer.
Each
URL
instance unambiguously identifies an Internet resource.
URL
declares several constructors with
URL(String s)
being the simplest. This
constructor creates a
URL
instance from the
String
argument passed to
s
and is
demonstrated as follows:
try
{