Java CookBook Learning Day2nd--HTTP (by Tim O'Brien)

上接:Java CookBook Learning Day1th--HTTP and WebDAV (by Tim O'Brien)

1.6. Debugging HTTP Communications

 1.6.1. Problem

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

 1.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/jcook/";

HttpMethod method=new GetMethod(url);

client.executeMethod(method);

String response=method.getResponseBodyAsString( );

System.out.println(reponse);

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>

 1.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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值