FileDocCategorySizeDatePackage
ConditionalGetExample.javaAPI DocExample3248Wed May 18 09:39:00 BST 2005com.discursive.jccook.httpclient

ConditionalGetExample

public class ConditionalGetExample extends Object

Fields Summary
String
entityTag
String
lastModified
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

		// Configure Logging
		System.setProperty("org.apache.commons.logging.Log", 
			"org.apache.commons.logging.impl.SimpleLog");
		System.setProperty("org.apache.commons.logging.simplelog.showdatetime", 
			"true");
		System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", 
			"debug");
		System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", 
			"debug");

		ConditionalGetExample example = new ConditionalGetExample();
		example.start();
	
private voidprocessResults(org.apache.commons.httpclient.HttpMethod method)

		if(method.getStatusCode() == HttpStatus.SC_NOT_MODIFIED ) {
			System.out.println( "Content not modified since last request" );
		} else {
			System.out.println( "Get Method retrieved content." );
			entityTag = retrieveHeader( method, "ETag" );
		  	lastModified = retrieveHeader( method, "Last-Modified" );
			System.out.println( "Entity Tag: " + entityTag );
			System.out.println( "Last Modified: " + lastModified );
		} 
	
private java.lang.StringretrieveHeader(org.apache.commons.httpclient.HttpMethod method, java.lang.String name)

		HeaderElement[] header = method.getResponseHeader(name).getElements();
		String value = "";
		if(header.length > 0) {
		 value = header[0].getName();
		}
		return value;
	
private voidsetHeaders(org.apache.commons.httpclient.HttpMethod method)

		method.setRequestHeader(new Header("If-None-Match", entityTag ) );
		method.setRequestHeader(new Header("If-Modified-Since", lastModified ) );
	
public voidstart()

	
	      

		HttpClient client = new HttpClient();
		HttpMethod method = new GetMethod("http://www.apache.org");

		for( int i = 0; i < 3; i++ ) {
			setHeaders(method);
			client.executeMethod(method);
			processResults(method);
			method.releaseConnection();
			method.recycle();
		}