Common Java Cookbook

Edition: 0.19

Download PDF or Read on Scribd

Download Examples (ZIP)

11.6. Debugging HTTP Communications

11.6.1. Problem

You need to see the low-level communications between the client and the server.

11.6.2. 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 - -Java version: 1.4.2_04
HttpClient - -Java vendor: Sun Microsystems Inc.
HttpClient - -Java class path: 
HttpClient - -Operating system name: Windows XP
HttpClient - -Operating system architecture: x86
HttpClient - -Operating system version: 5.1
HttpClient - -SUN 1.42: SUN (DSA key/parameter generation; DSA signing; SHA-1, \
             MD5  digests; SecureRandom; X.509 certificates; JKS keystore; \
             PKIX CertPathValidator; PKIX CertPathBuilder; LDAP, Collection \
             CertStores)
HttpClient - -SunJSSE 1.42: Sun JSSE provider(implements RSA Signatures, \
             PKCS12, SunX509 key/trust factories, SSLv3, TLSv1)
HttpClient - -SunRsaSign 1.42: SUN's provider for RSA signatures
HttpClient - -SunJCE 1.42: SunJCE Provider (implements DES, Triple DES, \
             AES, Blowfish, PBE, Diffie-Hellman, HMAC-MD5, HMAC-SHA1)
HttpClient - -SunJGSS 1.0: Sun (Kerberos v5)
HttpConnection - -HttpConnection.setSoTimeout(0)
HttpMethodBase - -Execute loop try 1
wire - ->> "GET /jccook/ HTTP/1.1[\r][\n]"
HttpMethodBase - -Adding Host request header
wire - ->> "User-Agent: Jakarta Commons-HttpClient/3.0final[\r][\n]"
wire - ->> "Host: www.discursive.com[\r][\n]"
wire - ->> "[\r][\n]"
wire - -<< "HTTP/1.1 200 OK[\r][\n]"
wire - -<< "Date: Thu, 06 May 2004 02:49:43 GMT[\r][\n]"
wire - -<< "Server: Apache/2.0.48 (Fedora)[\r][\n]"
wire - -<< "Last-Modified: Wed, 05 May 2004 02:51:37 GMT[\r][\n]"
wire - -<< "ETag: "a06d1-68-81486040"[\r][\n]"
wire - -<< "Accept-Ranges: bytes[\r][\n]"
wire - -<< "Content-Length: 104[\r][\n]"
wire - -<< "Content-Type: text/html; charset=UTF-8[\r][\n]"
HttpMethodBase - -Buffering response body
wire - -<< "<html>[\n]"
wire - -<< " <head>[\n]"
wire - -<< "  <title>JCCook Example</title>[\n]"
wire - -<< " </head>[\n]"
wire - -<< " <body>[\n]"
wire - -<< "  <h1>Hello World!</h1>[\n]"
wire - -<< " </body>[\n]"
wire - -<< "</html>"
HttpMethodBase - -Resorting to protocol version default close connection policy
HttpMethodBase - -Should NOT close connection, using HTTP/1.1.
<html>
 <head>
  <title>JCCook Example</title>
 </head>
 <body>
  <h1>Hello World!</h1>
 </body>
</html>

11.6.3. Discussion

The ability to see the communications between a browser and a server is a great diagnostic tool, and, throughout this chapter, the wire protocol logging properties have been used to provide some insight into the inner workings of HttpClient. There were four System properties set in the previous example:

org.apache.commons.logging.simplelog.log.httpclient.wire

Setting this property to debug causes an HttpClient instance to print out all traffic sent to and received from a web server.

org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient

Setting this property to debug configures HttpClient to print general debugging information. In the previous example, every line starting with HttpClient or HttpMethodBase is a debugging message configured by this setting.

org.apache.commons.logging.Log

Setting this property to org.apache.commons.logging.impl.SimpleLog configures HttpClient to log output to the console.

org.apache.commons.logging.simplelog.showdatetime

Setting this to true will cause the SimpleLog to print the date and time for every message.


Creative Commons License
Common Java Cookbook by Tim O'Brien is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License.
Permissions beyond the scope of this license may be available at http://www.discursive.com/books/cjcook/reference/jakartackbk-PREFACE-1.html. Copyright 2009. Common Java Cookbook Chunked HTML Output. Some Rights Reserved.