Java Reference
In-Depth Information
In addition to the
InputStream
, each
HttpURLConnection
object also has
an
OutputStream
, if requested. The following code requests an
OutputStream
for
the
HttpURLConnection
object.
OutputStream os = http.getOutputStream();
If you use the
OutputStream
to send data to the web server, then the HTTP request
will be a
POST
request. If you do not make use of the
OutputStream
, the request will
be a
GET
.
POST
requests are usually used when the user responds to a form. Chapter 7,
“Responding to Forms,” will show you how to use forms and
POST
requests.
HTTP Request Headers
Setting the values of HTTP request headers is another common task of an
HttpURLConnection
object. The web browser sends browser headers, or HTTP re-
quest headers, to the web server. These headers are most commonly used for the following
purposes:
• Identifying the type of web browser
• Transmitting any cookies
• Facilitating HTTP authentication
There are other things that can be accomplished with HTTP request headers; however,
these are the most common. HTTP authentication will be covered in Chapter 5, “Secure
HTTP Requests,” and cookies will be covered in Chapter 8, “Handling Sessions and Cookies”.
Setting the type of browser will be covered later in this section.
Setting HTTP Request Headers
The
HttpURLConnection
class provides several functions and methods that can
be used to access the HTTP request headers. These functions and methods are shown in
Table 4.1.