Java Reference
In-Depth Information
GET
POST
HEAD
PUT
DELETE
OPTIONS
TRACE
If it's some other method, then a java.net.ProtocolException , a subclass of
IOException , is thrown. However, it's generally not enough to simply set the request
method. Depending on what you're trying to do, you may need to adjust the HTTP
header and provide a message body as well. For instance, POST ing a form requires you
to provide a Content-length header. We've already explored the GET and POST methods.
Let's look at the other five possibilities.
Some web servers support additional, nonstandard request methods.
For instance, WebDAV requires servers to support PROPFIND , PROP
PATCH , MKCOL , COPY , MOVE , LOCK , and UNLOCK . However, Java doesn't
support any of these.
HEAD
The HEAD function is possibly the simplest of all the request methods. It behaves much
like GET . However, it tells the server only to return the HTTP header, not to actually
send the file. The most common use of this method is to check whether a file has been
modified since the last time it was cached. Example 7-15 is a simple program that uses
the HEAD request method and prints the last time a file on a server was modified.
Example 7-15. Get the time when a URL was last changed
import java.io.* ;
import java.net.* ;
import java.util.* ;
public class LastModified {
public static void main ( String [] args ) {
for ( int i = 0 ; i < args . length ; i ++) {
try {
URL u = new URL ( args [ i ]);
HttpURLConnection http = ( HttpURLConnection ) u . openConnection ();
http . setRequestMethod ( "HEAD" );
System . out . println ( u + " was last modified at "
Search WWH ::




Custom Search