Java Reference
In-Depth Information
The response from the web server, to the
GET
and
POST
requests is the same. In
both cases, the response will be an HTML file, image, or some other form of data. What is
returned depends on what the web server is programmed to return for the request it has
received. The usual response to a
POST
will be a HTML page that displays the result of the
form. For example, the response to a
POST
from an order form might be a HTML page that
contains the user's order number.
In the next few sections how the
GET
and
POST
requests work will be explained in
greater detail.
GET Requests
Choosing between
GET
and
POST
usually comes down to how much data must pass to
the web site.
GET
allows only a limited amount of data to be passed to the web server.
POST
allows a nearly infinite amount of data to be passed to the web server. However, if you are
writing an HTTP program to communicate with an existing web site, the choice is not yours.
You must conform to what that site expects. Therefore, most HTTP applications will need to
support a mix of
GET
and
POST
requests.
The
GET
request is good for when little, or no, additional information must be sent to the
web server with a request. For example, the following URL, if sent with a
GET
request, will
pass no data to the web server.
http://www.httprecipes.com/1/test.php
The above URL simply requests the
test.php
page and does not pass any arguments
on to the page. However, several arguments may need to be passed. What if the bot needed to
pass two arguments named “
first
” and “
last
”? The following URL would do this:
http://www.httprecipes.com/1/test.php?first=Jeff&last=Heaton
This would pass two arguments to the
test.php
page. As can be seen, passing argu-
ments with a
GET
request requires them to be appended onto the URL. The question mark
(?) indicates that the arguments have started. Each argument is the name of the argument,
followed by an equal sign (=), followed by the value of the argument. Each argument is sepa-
rated from the other arguments using an ampersand (&) symbol.
If there are a large number of arguments to pass,
GET
can be cumbersome. In such
cases, the
POST
request should be considered. Of course, as previously stated, if using an
existing web site, the bot must conform to what request type is already being used.