URLConnection class - Adv Java

Notice that the port is 1; this means that one was not explicitly set. Now that we have created a URL object, we want to retrieve the data associated with it. To access the actual bits or content information of a URL, we create a URL Connection object from it, using its openConnection( ) method, like this:

It returns a URLConnection object associated with the invoking URL object. It may throw an IOException.

URLConnection is an abstract class that represents an active connection to a resource specified by a URL. The URL Connection class has two different but related purposes. First, it provides more control over the interaction with a server (especially an HTTP server) than the URL class. With a URL Connection,

we can inspect the header sent by the server and respond accordingly. We can set the header fields used in the client request. We can use a URL Connection to download binary files. Finally, a URL Connection lets us send data back to a web server with POST or PUT and use other HTTP request methods. A program that uses the URL Connection class directly follows this basic sequence of steps:

  1. Construct a URL object.
  2. Invoke the URL object's open Connection( ) method to retrieve a URL Connection object for that URL.
  3. Configure the URLConnection.
  4. Read the header fields.
  5. Get an input stream and read data.
  6. Get an output stream and write data.
  7. Close the connection.

We don't always perform all these steps. For instance, if the default setup for a particular kind of URL is acceptable, then we're likely to skip step 3. If we only want the data from the server and don't care about any meta -information, or if the protocol doesn't provide any meta -information, we'll skip step 4. If we only want to receive data from the server but not send data to the server, we'll skip step 6. Depending on the protocol, steps 5 and 6 may be reversed or interlaced.

The single constructor for the URL Connection class is protected:

Consequently, unless we're sub -classing URLConnection to handle a new kind of URL (that is, writing a protocol handler), we can only get a reference to one of these objects through the openConnection( ) methods of the URL and URLStreamHandler classes. For example:

Reading Data from a Server

Here is the minimal set of steps needed to retrieve data from a URL using a URLConnection object:

  1. Construct a URL object.
  2. Invoke the URL object's openConnection( ) method to retrieve a URLConnection object for that URL.
  3. Invoke the URLConnection's getInputStream( ) method.
  4. Read from the input stream using the usual stream API.
  5. The getInputStream() method returns a generic InputStream that lets you read and parse the data that the server sends.
  6. public InputStream getInputStream( )

Example- Download a web page with a URLConnection

The differences between URL and URLConnection aren't apparent with just a simple input stream as in this example. The biggest differences between the two classes are:

  1. URLConnection provides access to the HTTP header.
  2. URLConnection can configure the request parameters sent to the server.
  3. URLConnection can write data to the server as well as read data from the server.

Reading the Header

HTTP servers provide a substantial amount of information in the header that precedes each response. For example, here's a typical HTTP header returned by an Apache web server:

  1. public String getContentType( )

    This method returns the MIME content type of the data. It relies on the web server to send a valid content type.

    For Example:

  2. public int getContentLength( )

    The get Content Length( ) method tells us how many bytes there are in the content. Many servers send Content -length headers only when they're transferring a binary file, not when transferring a text file. If there is no Content -length header, get Content Length() returns -1. The method throws no exceptions. It is used when we need to know exactly how many bytes to read or when we need to create a buffer large enough to hold the data in advance.

  3. public long getDate( )

    The getDate( ) method returns a long that tells us when the document was sent, in milliseconds since midnight, Greenwich Mean Time (GMT), January 1, 1970. We can convert it to a java.util.Date. For example:

    This is the time the document was sent as seen from the server; it may not agree with the time on our local machine. If the HTTP header does not include a Date field, getDate( ) returns 0.
  4. public long getExpiration( )

    Some documents have server -based expiration dates that indicate when the document should be deleted from the cache and reloaded from the server. get Expiration( ) is very similar to getDate( ), differing only in how the return value is interpreted. It returns a long indicating the number of milliseconds after 12:00 A.M., GMT, January 1, 1970, at which point the document expires.

    If the HTTP header does not include an Expiration field, get Expiration( )returns 0, which means 12:00 A.M., GMT, January 1, 1970. The only reasonable interpretation of this date is that the document does not expire and can remain in the cache indefinitely.

  5. public long getLastModified( )

    The final date method, getLastModified( ), returns the date on which the document was last modified. Again, the date is given as the number of milliseconds since midnight, GMT, January 1, 1970. If the HTTP header does not include a Last -modified field (and many don't), this method returns 0.


Retrieving Arbitrary Header Fields

The last six methods requested specific fields from the header, but there's no theoretical limit to the number of header fields a message can contain. The next five methods inspect arbitrary fields in a header. Indeed, the methods of the last section are just thin wrappers over the methods discussed here; we can use these methods to get header fields that Java's designers did not plan for. If the requested header is found, it is returned. Otherwise, the method returns null.

public String getHeaderField(String name)

The getHeaderField() method returns the value of a named header field. The name of the header is not case -sensitive and does not include a closing colon. For example, to get the value of the Content-type and Content -encoding header fields of a URLConnection object uc, we could write:

To get the Date, Content -length, or Expires headers, you'd do the same:

These methods all return String, not int or long as the getContentLength( ), getExpirationDate(), getLastModified( ), and getDate( ) methods of the last section did. If we're interested in a numeric value, convert the String to a long or an int.

Do not assume the value returned by get HeaderField() is valid. We must check to make sure it is non-null.

This method returns the key (that is, the field name: for example, Content -length or Server) of the nth header field. The request method is header zero and has a null key. The first header is one. For example, to get the sixth key of the header of the URLConnection uc, we would write:

This method returns the value of the nth header field. In HTTP, the request method is header field zero and the first actual header is one. Example below uses this method in conjunction with get Header FieldKey( ) to print the entire HTTP header.

Besides Date, Last -modified, and Content -type headers, this server also provides Server, Title, and Link headers. Other servers may have different sets of headers.

This method first retrieves the header field specified by the name argument and tries to convert the string to a long that specifies the milliseconds since midnight, January 1, 1970, GMT. getHeader FieldDate() can be used to retrieve a header field that represents a date: for example, the Expires, Date, or Last -modified headers. To convert the string to an integer, getHeader FieldDate() uses the parseDate( ) method of java.util.Date.

The parseDate() method does a decent job of understanding and converting most common date formats, but it can be stumped —for instance, if we ask for a header field that contains something other than a date. If parseDate( ) doesn't understand the date or if getHeader FieldDate( ) is unable to find the requested header field, getHeaderFieldDate( ) returns the default argument. For example:

We can use the methods of the java.util.Date class to convert the long to a String.

This method retrieves the value of the header field name and tries to convert it to an int. If it fails, either because it can't find the requested header field or because that field does not contain a recognizable integer, get Header FieldInt( ) returns the default argument. This method is often used to retrieve the Content-length field. For example, to get the content length from a URLConnection uc, you would write:

In this code fragment, getHeader FieldInt( ) returns -1 if the Contentlengt header isn't present.

Configuring the Connection

The URLConnection class has seven protected instance fields that define exactly how the client makes the request to the server. These are:

For instance, if doOutput is true, we'll be able to write data to the server over this URLConnection as well as read data from it. If useCaches is false, the connection bypasses any local caching and downloads the file from the server afresh.

Since these fields are all protected, their values are accessed and modified via obviously named setter and getter methods:

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Adv Java Topics