Mega Code Archive

 
Categories / Java Book / 006 Networking
 

0356 URL

The Uniform Resource Locator (URL) locates all of the resources of the Net. A URL has four components: the protocol, separated from the rest of the locator by a colon (:). Common protocols are HTTP, FTP, gopher, and file. the host name or IP address, this is delimited on the left by double slashes (//) and on the right by a slash (/) or optionally a colon (:). the port number, an optional parameter, delimited on the left from the host name by a colon (:) and on the right by a slash (/). It defaults to port 80, the predefined HTTP port. file path. Java's URL class has several constructors: URL(String urlSpecifier) throws MalformedURLException create URL from string URL(String protocolName, String hostName, int port, String path) throws MalformedURLException break up the URL into its component parts URL(String protocolName, String hostName, String path) throws MalformedURLException break up the URL into its component parts URL(URL urlObj, String urlSpecifier) throws MalformedURLException use an existing URL as a reference context and then create a new URL from that context. To access the actual content, create a URLConnection object from it, using its openConnection( ) method, like this: openConnection( ) has the following general form: