November 2004
Intermediate to advanced
400 pages
10h 31m
English
You need to retrieve the same content more than once, and you would like to have the server only send the content if it has changed since the last request.
Create a GetMethod and set the
If-None-Match and
If-Modified-Since headers; these two headers
will instruct the server to refrain from sending content if the
content has not been altered since the last request. Example 11-1 makes three separate requests for the same URL
(http://www.apache.org), and,
because the content remains static, it is only sent in the response
body of the first request.
Example 11-1. Requesting information with a conditional GET
importjava.io.IOException;importorg.apache.commons.httpclient.Header;importorg.apache.commons.httpclient.HeaderElement;importorg.apache.commons.httpclient.HttpClient;importorg.apache.commons.httpclient.HttpException;importorg.apache.commons.httpclient.HttpMethod;importorg.apache.commons.httpclient.HttpStatus;importorg.apache.commons.httpclient.methods.GetMethod;publicclassConditionalGetExample {publicstaticvoidmain(String[] args)throwsHttpException, IOException { ConditionalGetExample example =newConditionalGetExample( ); example.start( ); } String entityTag = ""; String lastModified = "";publicvoidstart( )throwsHttpException, IOException { HttpClient client =newHttpClient( ); HttpMethod method =newGetMethod("http://www.apache.org");for(inti = 0; i < 3; i++ ) { setHeaders(method); ...
Read now
Unlock full access