11.6. Debugging HTTP Communications
Problem
You need to see the low-level communications between the client and the server.
Solution
Set four System
variables that control logging,
and HttpClient
will
produce debugging statements dealing with environment information,
SSL configuration information, and the raw data sent to and received
from the server. The following example sets the four
System
properties that control
HttpClient
debugging output:
import
org.apache.commons.httpclient.HttpClient;import
org.apache.commons.httpclient.HttpException;import
org.apache.commons.httpclient.HttpMethod;import
org.apache.commons.httpclient.methods.GetMethod; String logging = "org.apache.commons.logging"; // Configure Logging System.setProperty(logging + ".Log", logging + ".impl.SimpleLog"); System.setProperty(logging + ".logging.simplelog.showdatetime", "true"); System.setProperty(logging + ".simplelog.log.httpclient.wire", "debug"); System.setProperty(logging + ".simplelog.log.org.apache.commons.httpclient", "debug"); HttpClient client =new
HttpClient( ); String url = "http://www.discursive.com/jccook/"; HttpMethod method =new
GetMethod( url ); client.executeMethod( method ); String response = method.getResponseBodyAsString( ); System.out.println( response ); method.releaseConnection( ); method.recycle( );
This code executes a simple GetMethod
and produces
the following debugging output, which contains environment
information and a log of all data sent and received from the server:
HttpClient ...
Get Jakarta Commons Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.