In HTTP 1.0 and later, the client sends the server not only a request line, but also a MIME header. For example, here’s the MIME header that Netscape Navigator 4.6 for Windows uses:
Connection: Keep-Alive User-Agent: Mozilla/4.6 [en] (WinNT; I) Host: login.metalab.unc.edu:38309 Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */* Accept-Encoding: gzip Accept-Language: en Accept-Charset: iso-8859-1,*,utf-8
A simple web server can ignore this. A more sophisticated web server can use this information to serve different pages to different clients, to get and set cookies, to authenticate users through passwords, and more. All of this is done by placing different fields in the MIME headers that the client sends and the server responds with.
It’s important to understand that this is not the MIME header that the server sends to the client, and that is read by the various getHeaderField( )
and getHeaderFieldKey( )
methods discussed previously. This is the MIME header that the client sends to the server.
Each concrete subclass of URLConnection
sets a
number of different name-value pairs in its MIME header by default.
(Really, only HttpURLConnection
does this, since
HTTP is the only major protocol that uses MIME headers in this way.)
For instance, here’s the MIME header that a connection from the
SourceViewer2
program of Example 15.1 sends:
User-Agent: Java1.3beta Host: login.metalab.unc.edu:38358 Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2 Connection: close
As you can see, it’s a little simpler than the one Netscape
Navigator sends, and it has a different user agent and accepts
different kinds of files. However, you can modify these and add new
fields before connecting. In Java 1.3 and earlier, you do this with
the static URLConnection.setDefaultRequestProperty( )
and URLConnection.getDefaultRequestProperty( )
methods:
public String getDefaultRequestProperty(String name) public static void setDefaultRequestProperty(String name, String value)
The setDefaultRequestProperty( )
method adds a
field with a specified name and value to the MIME header of all
subsequently created URLConnection
objects. The
getDefaultRequestProperty( )
method returns the
value of the named field of the MIME header used by all
URLConnection
objects. For example, if you wanted
to add Accept-language and Accept-charset headers to all your
outgoing connections, you’d use this code before you opened the
connection:
URLConnection.setDefaultRequestProperty("Accept-Language", "en"); URLConnection.setDefaultRequestProperty("Accept-Charset", "iso-8859-1,utf-8");
This is frankly a rather strange approach. For instance, you normally
wouldn’t use an Accept-language header when requesting a binary
file such as a JPEG image. However, you’ll get one anyway
because this is pretty much an all-or-nothing approach. It would make
more sense to set custom MIME headers on a connection-by-connection
basis. Indeed, Sun has realized this, and both of these methods are
deprecated in Java 1.3. Instead, their functionality is provided by
the setRequestProperty( )
and
getRequestProperty( )
instance methods:
public void setRequestProperty(String name, String value) // Java 1.3 public String getRequestProperty(String name) // Java 1.3
Both the instance and static methods really have meaning only when
the URL being connected to is an http URL, since only the HTTP protocol makes
use of MIME headers. While they could possibly have other meanings in
other protocols, such as NNTP, this is really just an example of poor
API design. These methods should be part of the more specific
HttpURLConnection
class and not the generic
URLConnection
class.
The setRequestProperty( )
method adds a field to
the MIME header of this URLConnection
with a
specified name and value. This method can be used only before the
connection is opened. It throws an
IllegalAccessError
if the connection is already
open. The getRequestProperty( )
method returns the
value of the named field of the MIME header used by this
URLConnection
.
For example, web servers and clients store some limited persistent information by using cookies. A cookie is simply a name-value pair. The server sends a cookie to a client using the response MIME header. From that point forward, whenever the client requests a URL from that server, it includes a Cookie field in the MIME request header. That field looks like this:
Cookie: username=elharo; password=ACD0X9F23JJJn6G; session=100678945
This particular Cookie field would send three name-value pairs to the
server. There’s no limit to the number of name-value pairs that
can be included in any one cookie. Given a
URLConnection
object uc
, you
could add this cookie to the connection like this:
uc.setRequestProperty("Cookie", "username=elharo; password=ACD0X9F23JJJn6G; session=100678945");
3.146.105.137