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[ ][ ]" HttpMethodBase - -Adding Host request header wire - ->> "User-Agent: Jakarta Commons-HttpClient/3.0final[ ][ ]" wire - ->> "Host: www.discursive.com[ ][ ]" wire - ->> "[ ][ ]" wire - -<< "HTTP/1.1 200 OK[ ][ ]" wire - -<< "Date: Thu, 06 May 2004 02:49:43 GMT[ ][ ]" wire - -<< "Server: Apache/2.0.48 (Fedora)[ ][ ]" wire - -<< "Last-Modified: Wed, 05 May 2004 02:51:37 GMT[ ][ ]" wire - -<< "ETag: "a06d1-68-81486040"[ ][ ]" wire - -<< "Accept-Ranges: bytes[ ][ ]" wire - -<< "Content-Length: 104[ ][ ]" wire - -<< "Content-Type: text/html; charset=UTF-8[ ][ ]" HttpMethodBase - -Buffering response body wire - -<< "<html>[ ]" wire - -<< " <head>[ ]" wire - -<< " <title>JCCook Example</title>[ ]" wire - -<< " </head>[ ]" wire - -<< " <body>[ ]" wire - -<< " <h1>Hello World!</h1>[ ]" wire - -<< " </body>[ ]" 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>
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.
18.118.226.66