Java Reference
In-Depth Information
In practice, POST is vastly overused on the Web today. Any safe operation that does not
commit the user to anything should use GET rather than POST . Only operations that
commit the user should use POST .
One sometimes mistaken reason for preferring POST over GET is when forms require
large amounts of input. There's an outdated misconception that browsers can only work
with query strings of a few hundred bytes. Although this was true in the mid-1990s,
today all major browsers are good up to URL lengths of at least 2,000 characters. If you
have more form data to submit than that, you may indeed need to support POST ; but
safe operations should still prefer GET for nonbrowser clients. This is less common than
you might think, though. You usually only exceed those limits if you're uploading data
to the server to create a new resource, rather than merely locating an existing resource
on the server; and in these cases POST or PUT is usually the right answer anyway.
In addition to these four main HTTP methods, a few others are used in special circumā€
stances. The most common such method is HEAD , which acts like a GET except it only
returns the header for the resource, not the actual data. This is commonly used to check
the modification date of a file, to see whether a copy stored in the local cache is still
valid.
The other two that Java supports are OPTIONS , which lets the client ask the server what
it can do with a specified resource; and TRACE , which echoes back the client request for
debugging purposes, especially when proxy servers are misbehaving. Different servers
recognize other nonstandard methods including COPY and MOVE , but Java does not send
these.
The URL class described in the previous chapter uses GET to communicate with HTTP
servers. The URLConnection class (coming up in the Chapter 7 ) can use all four of these
methods.
The Request Body
The GET method retrieves a representation of a resource identified by a URL. The exact
location of the resource you want to GET from a server is specified by the various parts
of the path and query string. How different paths and query strings map to different
resources is determined by the server. The URL class doesn't really care about that. As
long as it knows the URL, it can download from it.
POST and PUT are more complex. In these cases, the client supplies the representation of
the resource, in addition to the path and the query string. The representation of the
resource is sent in the body of the request, after the header. That is, it sends these four
items in order:
1. A starter line including the method, path and query string, and HTTP version
Search WWH ::




Custom Search