Java Reference
In-Depth Information
The ContentConnection class inherits from StreamConnection and provides three new
methods: getEncoding , getLength , and getType . This example uses the getLength method to
determine the number of bytes being returned by the remote server; unfortunately,
there's no guarantee that the server will actually report this information. Consequently,
this example tests the returned content length; if it's undefined, the code will read the
data in 512-byte chunks. Either way, the code creates a new image with the returned data
and closes both the input stream and ContentConnection . This code is encapsulated in a
runnable method, run , because in practice you're likely to use threads for all of your I/O.
(I show you a concrete example of this in the “Putting HTTP to Work” section, which
details a full application that uses HTTP.)
The ContentConnection class is a high-level abstraction of the kind of metadata
that HTTP provides, and it lets you perform only a fetch of remote content. Using the
ContentConnection , you specify a remote resource when you create the connection
using the Connector class. Under the hood, the platform makes the desired request
using the HTTP method GET and the connection you specify when you request the
DataInputStream from which the remote resource will be read, giving you no control
over the semantics of the HTTP request itself. Once the platform has completed the
request, you can learn only three things about the response: how the response was
encoded (using the getEncoding method), the number of bytes returned (using the
getLength method), and the Multipurpose Internet Mail Extensions (MIME) type of the
method (using the getType method).
Note The content type and other information about the response are only available if the server provides
them; your application must be prepared to deal with the possibility that a server won't return this informa-
tion (unless you have control over the server implementation as well, and even then you would need to
handle any errors that arise in your application from a lack of this metadata).
Because ContentConnection is such a simple interface, it's ideal when your applica-
tion needs only to obtain the contents of a remote resource. It is, however, woefully
inadequate for more sophisticated uses of HTTP, because it does not provide access to
most of the metadata that accompanies HTTP requests and responses, nor does it let
you specify the method HTTP invokes when making the request. For finer-grained
access to the HTTP protocol, you need to use instances of the HttpConnection class—
a class that inherits from ContentConnection . (In fact, when you invoke Connector.open
on a URL with the http scheme, the instance returned is the HttpConnection , not the
ContentConnection the code in Listing 12-8 suggests.)
Reflecting the underlying connection with a remote HTTP server, an HttpConnection
instance can be in one of three states:
 
Search WWH ::




Custom Search