Java Reference
In-Depth Information
Accept-Ranges: bytes
Content-Length: 80366
Content-Type: text/html; charset=utf-8
Cache-Control: max-age=14400
Expires: Sat, 04 May 2013 15:28:26 GMT
Vary: Accept-Encoding
Keep-Alive: timeout=3, max=100
Connection: Keep-Alive
Besides the headers with named getter methods, this server also provides Server, Accept-
Ranges, Cache-control, Vary, Keep-Alive, and Connection headers. Other servers may
have different sets of headers.
public long getHeaderFieldDate(String name, long default)
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.
getHeaderFieldDate()
can be used to retrieve a header field that represents
a date (e.g., the Expires, Date, or Last-modified headers). To convert the string to an
integer,
getHeaderFieldDate()
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 you ask for a header field that
contains something other than a date. If
parseDate()
doesn't understand the date or if
getHeaderFieldDate()
is unable to find the requested header field,
getHeaderField
Date()
returns the
default
argument. For example:
Date
expires
=
new
Date
(
uc
.
getHeaderFieldDate
(
"expires"
,
0
));
long
lastModified
=
uc
.
getHeaderFieldDate
(
"last-modified"
,
0
);
Date
now
=
new
Date
(
uc
.
getHeaderFieldDate
(
"date"
,
0
));
You can use the methods of the
java.util.Date
class to convert the
long
to a
String
.
public int getHeaderFieldInt(String name, int default)
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,
getHeaderFieldInt()
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:
int
contentLength
=
uc
.
getHeaderFieldInt
(
"content-length"
,
-
1
);
In this code fragment,
getHeaderFieldInt()
returns -1 if the Content-length header
isn't present.
Caches
Web browsers have been caching pages and images for years. If a logo is repeated on
every page of a site, the browser normally loads it from the remote server only once,