Java Reference
In-Depth Information
Connection: close
Content-Type: text/html; charset=iso-8859-1
There's a lot of information there. In general, an HTTP header may include the content
type of the requested document, the length of the document in bytes, the character set
in which the content is encoded, the date and time, the date the content expires, and the
date the content was last modified. However, the information depends on the server;
some servers send all this information for each request, others send some information,
and a few don't send anything. The methods of this section allow you to query a URL
Connection to find out what metadata the server has provided.
Aside from HTTP, very few protocols use MIME headers (and technically speaking,
even the HTTP header isn't actually a MIME header; it just looks a lot like one). When
writing your own subclass of URLConnection , it is often necessary to override these
methods so that they return sensible values. The most important piece of information
you may be lacking is the content type. URLConnection provides some utility methods
that guess the data's content type based on its filename or the first few bytes of the data
itself.
Retrieving Specific Header Fields
The first six methods request specific, particularly common fields from the header.
These are:
• Content-type
• Content-length
• Content-encoding
• Date
• Last-modified
• Expires
public String getContentType()
The getContentType() method returns the MIME media type of the response body. It
relies on the web server to send a valid content type. It throws no exceptions and returns
null if the content type isn't available. text/html will be the most common content type
you'll encounter when connecting to web servers. Other commonly used types include
text/plain , image/gif , application/xml , and image/jpeg .
If the content type is some form of text, this header may also contain a character set part
identifying the document's character encoding. For example:
Content-type: text/html; charset=UTF-8
Or:
Search WWH ::




Custom Search