URLConnection Class in Java
The URL provides a reasonably intelligible form to uniquely identify or address information on the Internet. URLs are ubiquitous, every browser uses them to identify information on the Web. Within Java’s network class library, the URL class provides a simple, concise API to access information across the Internet using URLs.
A URL specification is based on four components. The first is the protocol to use, separated from
the rest of the locator by a colon (:). Common protocols are HTTP, FTP, gopher, and file, although these days almost everything is being done via HTTP. The second component is the hostname or IP address of the host to use; this is delimited on the left by double slashes (//) and on the right by a slash (/) or optionally a colon (:). The third component, the port number, is an optional parameter, delimited on the left from the hostname by a colon (:) and on the right by a slash (/). (It defaults to port 80, the predefined HTTP port; thus, “:80” is redundant.) The fourth part is the actual file path. Most HTTP servers will append a file named index.html or index.htm to URLs that refer directly to a directory resource.
Java’s URL class has several constructors; each can throw a MalformedURLException. One commonly used form specifies the URL with a string that is identical to what you see displayed in a browser:
URL(String urlSpecifier) throws MalformedURLException
The next two forms of the constructor allow you to break up the URL into its component parts:
URL(String protocolName, String hostName, int port, String path) throws MalformedURLException URL(String protocolName, String hostName, String path) throws MalformedURLException
It is a general-purpose class for accessing the attributes of a remote resource. Once you make a connection to a remote server, you can use URLConnection to inspect the properties of the remote object before actually transporting it locally. These attributes are exposed by the HTTP protocol specification and, as such, only make sense for URL objects that are using the HTTP protocol.
int getContentLength(): Returns the size in bytes of the content associated with the resource. If the length is unavailable, –1 is returned.
String getContentType(): Returns the type of content found in the resource. This is the value of the content-type header field.
long getDate(): Returns the time and date of the response represented in terms of milliseconds.
long getExpiration(): Returns the expiration time and date of the resource represented in terms of milliseconds
String getHeaderField(int idx): Returns the value of the header field at index idx. Returns null if the value of idx exceeds the number of fields.
String getHeaderField(String fieldName): Returns the value of header field whose name is specified by fieldName. Returns null if the specified name is not found.
String getHeaderFieldKey(int idx): Returns the header field key at index idx. Returns null if the value of idx exceeds the number of fields.
Map<String, List<String>> getHeaderFields(): Returns a map that contains all of the header fields and values.
long getLastModified(): Returns the time and date, represented in terms of milliseconds.
InputStream getInputStream() throws IOException: Returns an InputStream that is linked to the resource. This stream can be used to obtain the content of the resource.