﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-guangnian0412's BLOG-随笔分类-Jakarta Commons</title><link>http://www.blogjava.net/guangnian0412/category/9444.html</link><description>Java in my life</description><language>zh-cn</language><lastBuildDate>Tue, 27 Feb 2007 10:25:00 GMT</lastBuildDate><pubDate>Tue, 27 Feb 2007 10:25:00 GMT</pubDate><ttl>60</ttl><item><title>[Commons Logging]使用一个抽象的Logging接口（From Jarkata Commons Cookbook 7.10）</title><link>http://www.blogjava.net/guangnian0412/archive/2006/04/24/42874.html</link><dc:creator>guangnian</dc:creator><author>guangnian</author><pubDate>Mon, 24 Apr 2006 10:32:00 GMT</pubDate><guid>http://www.blogjava.net/guangnian0412/archive/2006/04/24/42874.html</guid><wfw:comment>http://www.blogjava.net/guangnian0412/comments/42874.html</wfw:comment><comments>http://www.blogjava.net/guangnian0412/archive/2006/04/24/42874.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/guangnian0412/comments/commentRss/42874.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/guangnian0412/services/trackbacks/42874.html</trackback:ping><description><![CDATA[
		<font face="Georgia">   （Jarkata 的 Commons Logging 包现在已经被用在几乎所有的开源项目之中，它可以使你开发的系统工作在不同的日志框架下，包括Sun的logging框架和Apache Log4j。现在Commons Logging + Apache Log4j 的身影是随处可见，Commons Logging 的易用与Log4j的强大功能形成了绝配。）<br /><br />问题：<br />      你正在写一个可重用的代码库，而你不知道你的代码在哪里并且是如何工作的。你需要一个抽象的日志接口来写入日志信息，因为你不能确定Log4j或者是JDK 1.4 logging的存在性。<br /><br />解决：<br />        通过Jakarta Commons Logging 的Log 接口来记录信息，然后依靠Commons Logging自身来决定在运行时使用哪种具体的日志框架。下面的代码使用了Log接口来记录trace，debug，info，warning，error和fatal信息：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.apache.commons.logging.LogFactory;<br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.apache.commons.logging.Log<br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);">Log log </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> LogFactory.getLog( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">com.discursive.jccook.SomeApp</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> ); <br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( log.isTraceEnabled( ) ) {<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);">    log.trace( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">This is a trace message</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> );<br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( log.isDebugEnabled( ) ) {<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">    log.debug( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">This is a debug message</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> );<br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">}<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">log.info( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">This is an informational message</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> );<br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);">log.warn( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">This is a warning</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> );<br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);">log.error( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">This is an error</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> );<br /></span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);">log.fatal( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">This is fatal</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> );</span></div>      LogFactory.getInstance() 返回一个Log接口的具体实现，这个实现与底层具体的日志框架相对应。例如，如果你的系统是使用Apache Log4j ，一个Log4JLogger将被返回，对应于Log4J category </font>
		<font>
				<font face="Georgia">
						<span style="color: rgb(0, 0, 0);">
						</span>
						<span style="color: rgb(0, 0, 0);">com.discursive.jccook.SomeApp 。<br /><br />讨论：<br />        一个可重用代码库的开发者不能预知其代码库将在何时何地被用到，而现在有很多的日志框架可以使用，所以当开发可重用代码库的时候，使用Commons Logging 是非常明智的，例如Jakarta Commons 组件。当调用LogFactory.getInstance()方法的时候，Commons Logging 将通过系统属性和classpath中的类库来决定和管理适当的日志框架。对于一个小型可重用组件的开发者来说，进行日志记录只需要调用Log接口。而配置底层日志框架的负担，就转移到使用其组件库的开发者身上。<br /><br />参考：<br />        7.11节详细的说明了Commons Logging在运行时确定适当日志框架的算法。<br /><br /></span>
				</font>
		</font>
		<font face="Georgia">     <br /></font>
<img src ="http://www.blogjava.net/guangnian0412/aggbug/42874.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/guangnian0412/" target="_blank">guangnian</a> 2006-04-24 18:32 <a href="http://www.blogjava.net/guangnian0412/archive/2006/04/24/42874.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[HttpClient] 创建一个HTTP POST 请求 (from Jakarta Commons cookbook 11.7 )</title><link>http://www.blogjava.net/guangnian0412/archive/2006/04/11/40529.html</link><dc:creator>guangnian</dc:creator><author>guangnian</author><pubDate>Tue, 11 Apr 2006 14:41:00 GMT</pubDate><guid>http://www.blogjava.net/guangnian0412/archive/2006/04/11/40529.html</guid><wfw:comment>http://www.blogjava.net/guangnian0412/comments/40529.html</wfw:comment><comments>http://www.blogjava.net/guangnian0412/archive/2006/04/11/40529.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.blogjava.net/guangnian0412/comments/commentRss/40529.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/guangnian0412/services/trackbacks/40529.html</trackback:ping><description><![CDATA[
		<font face="Georgia">问题：<br />        你需要使用HTTP POST 方法来向一个servlet传递参数。<br /><br />讨论：<br />        创建一个 PostMethod 对象，然后调用 setParameter() 或 addParameter() 方法设置参数。 PostMethod 对象将会传送一个 Content-Type 头为 application/x-www-form-urlencoded 的请求，并且参数将在请求body中被传送。在下列的例子中演示了用 PostMethod 对象传递参数的用法：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.apache.commons.httpclient.HttpClient;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.apache.commons.httpclient.HttpException;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.apache.commons.httpclient.NameValuePair;<br /></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.apache.commons.httpclient.methods.PostMethod;<br /><br />HttpClient client </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> HttpClient( );<br /><br /></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> Create POST method</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">String url </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">http://www.discursive.com/cgi-bin/jccook/param_list.cgi</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br />PostMethod method </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> PostMethod( url );<br /><br /></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> Set parameters on POST    </span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">method.setParameter( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">test1</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Hello World</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> );<br />method.addParameter( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">test2</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">This is a Form Submission</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> );<br />method.addParameter( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Blah</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Whoop</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> );<br />method.addParameter( </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> NameValuePair( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Blah</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Whoop2</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> ) );<br /><br /></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> Execute and print response</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">client.executeMethod( method );<br />String response </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> method.getResponseBodyAsString( );<br />System.out.println( response );<br /><br />method.releaseConnection( );</span></div>       </font>
		<font>
				<font face="Georgia">
						<span style="color: rgb(0, 0, 0);">param_list.cgi CGI脚本会对所以接收到的参数进行回显，从下面的输出中，你可以看到传递给CGI脚本的三个参数：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">These are the parameters I received:<br /><br />test1:<br />  Hello World<br />test2:<br />  This is a Form Submission<br />Blah:<br />  Whoop<br />  Whoop2<br /></span></div>       有几种方法来在一个PostMethod对象中设置参数。最直接的方法就是调用setParameter()方法，并传递两个字符串给它：参数的名称和参数值。setParameter()方法将会替代任何已经存在的同名参数。但是，如果一个同名的参数已经存在一个PostMethod对象中，addParameter()将会加入另一个同名参数值；addParameter()方法同样接受两个String：参数名和参数值。另一种方法，这两个方法同样接受一个包装了参数名和参数值的NameValuePair对象。在前面的例子中，通过addParameter()方法，用参数名Blah传递了两个值，第一次用两个String作为参数，第二次用一个NameValuePair对象作为参数。<br /></span>
				</font>
		</font>
<img src ="http://www.blogjava.net/guangnian0412/aggbug/40529.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/guangnian0412/" target="_blank">guangnian</a> 2006-04-11 22:41 <a href="http://www.blogjava.net/guangnian0412/archive/2006/04/11/40529.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[HttpClient] 在查询字符串中传送参数 （from Jakarta Commons Cookbook 11。4）</title><link>http://www.blogjava.net/guangnian0412/archive/2006/04/04/39235.html</link><dc:creator>guangnian</dc:creator><author>guangnian</author><pubDate>Tue, 04 Apr 2006 11:19:00 GMT</pubDate><guid>http://www.blogjava.net/guangnian0412/archive/2006/04/04/39235.html</guid><wfw:comment>http://www.blogjava.net/guangnian0412/comments/39235.html</wfw:comment><comments>http://www.blogjava.net/guangnian0412/archive/2006/04/04/39235.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/guangnian0412/comments/commentRss/39235.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/guangnian0412/services/trackbacks/39235.html</trackback:ping><description><![CDATA[
		<font face="Georgia">问题：<br />        你需要在一个URL中传送查询参数。<br /><br />解答：<br />        使用一个HttpMethod实例的setQueryString()方法来设置查询字符串。使用URIUtil类对包含在URL中的文本进行编码。下面的例子在查询字符串中放入了两个参数：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.apache.commons.httpclient.HttpClient;<br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.apache.commons.httpclient.HttpException;<br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.apache.commons.httpclient.HttpMethod;<br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.apache.commons.httpclient.NameValuePair;<br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.apache.commons.httpclient.methods.GetMethod;<br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.apache.commons.httpclient.util.URIUtil;<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);">HttpClient client </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> HttpClient( );<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);">String url </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">http://www.discursive.com/cgi-bin/jccook/param_list.cgi</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);">HttpMethod method </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> GetMethod( url );<br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 用setQueryString()来设置查询字符串</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);">15</span> <b><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">method.setQueryString(URIUtil.encodeQuery(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">test1=O Reilly&amp;blah=Whoop</span><span style="color: rgb(0, 0, 0);">"</span></b><span style="color: rgb(0, 0, 0);"><b>));</b><br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);">System.out.println( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">With Query String: </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> method.getURI( ) );<br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);">client.executeMethod( method );<br /></span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);">System.out.println( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Response:\n </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> method.getResponseBodyAsString( ) );<br /></span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);">method.releaseConnection( );</span></div>       </font>
		<font>
				<font face="Georgia">
						<span style="color: rgb(0, 0, 0);">param_list.cgi  CGI脚本只是简单的回显接收到的所以参数，在下面的输出中，你可以看到URIUtil如何对第一个参数进行编码：<br />       <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">With Query String: http:</span><span>//www.discursive.com/cgi-bin/jccook/param_list.</span><span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">cgi</span><b><span style="color: rgb(0, 0, 0);">?</span></b><span style="color: rgb(0, 0, 0);">test1</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">O</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">20Reilly</span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 0);">blah</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">Whoop<br />Response:<br /> These are the parameters I received:<br /><br />test1:<br />  O Reilly<br />blah:<br />  Whoop<br /></span></div>      提示：你不必在setQueryString()方法中加入？号，当HttpClient实例执行executeMethod()方法时，它会被自动加入。<br /><br />讨论：<br />         在前面的例子中，HttpMethod的setQueryString()方法是一次性将整个查询字符串加进去，但是还有另外一种选择：通过一个NameValuePair对象的数组来设置查询字符串。当一个NameValuePair[]传入setQueryString()方法中时，HttpMethod实例会从数组中取出每一个NameValuePair对象，然后创建一系列用&amp;号分割的参数。这种方法使程序代码更加干净，因为你不必连接字符串来传递多个参数。下面的例子用NameValuePair对象，与前一个例子设置了同样的参数：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 用NameValuePair对象设置查询参数</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 128, 0);"></span><span style="color: rgb(0, 0, 0);">HttpMethod method </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> GetMethod( url );<br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);">NameValuePair pair </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 128);"></span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> NameValuePair( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">test1</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, URIUtil.encodeQuery( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">O Reilly</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> ) );<br /></span><span style="color: rgb(0, 128, 128);"> 4</span><span style="color: rgb(0, 0, 0);">NameValuePair pair2 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 128);"></span><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> NameValuePair( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">blah</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, URIUtil.encodeQuery( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Whoop</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> ) );<br /></span><span style="color: rgb(0, 128, 128);"> 5</span><span style="color: rgb(0, 0, 0);">NameValuePair[] pairs </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> NameValuePair[] { pair, pair2 };<br /></span><span style="color: rgb(0, 128, 128);"> 6</span><span style="color: rgb(0, 0, 0);">method.setQueryString( pairs );<br /></span><span style="color: rgb(0, 128, 128);"> 7</span><span style="color: rgb(0, 0, 0);">System.out.println( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">With NameValuePairs: </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> method.getURI( ) );<br /></span><span style="color: rgb(0, 128, 128);"> 8</span><span style="color: rgb(0, 0, 0);">client.executeMethod( method );<br /></span><span style="color: rgb(0, 128, 128);"> 9</span><span style="color: rgb(0, 0, 0);"> System.out.println( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Response:\n </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> method.getResponseBodyAsString( ) );<br /></span><span style="color: rgb(0, 128, 128);"> 10</span><span style="color: rgb(0, 0, 0);">method.releaseConnection( );</span></div>        根据RFC1738，URL只能够包含字母和数字字符：[0-9,a-z,A-Z]和一些特殊字符。如果你需要在参数中传送一些URL所不允许的字符，你就需要对你的字符串进行编码，以符合RFC1738的规定。URIUtil类有一个方法encodeQuery()能够对前面例子中的"O Reilly"进行编码。下面的代码展示了用URIUtil类来对包含在URL中的字符串进行编码：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);">1</span> <span style="color: rgb(0, 0, 0);">String encoded1 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> URIUtil.encodeQuery( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&lt;test&gt;=O'Connell</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> );<br /></span><span style="color: rgb(0, 128, 128);">2</span> <span style="color: rgb(0, 0, 0);">String encoded2 </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> URIUtil.encodeQuery( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">one:two=thr ee#</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> );<br /></span><span style="color: rgb(0, 128, 128);">3</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">4</span> <span style="color: rgb(0, 0, 0);">String decoded </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> URIUtil.decode( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Hello%20World%3F</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> );<br /></span><span style="color: rgb(0, 128, 128);">5</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">6</span> <span style="color: rgb(0, 0, 0);">System.out.println( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Encoded: </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> encoded1 );<br /></span><span style="color: rgb(0, 128, 128);">7</span> <span style="color: rgb(0, 0, 0);">System.out.println( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Encoded: </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> encoded2 );<br /></span><span style="color: rgb(0, 128, 128);">8</span> <span style="color: rgb(0, 0, 0);">System.out.println( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Decoded: </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> decoded );</span></div><br />        这个简单的例子用URIUtil类对两个字符串进行了编码，并对一个经过编码的字符串进行解码。下面的输出展示了每个转换的结果：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">Encoded: </span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">3ctest</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">e3</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">O</span><span style="color: rgb(0, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">Connell</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 0);">Encoded: one</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">3atwo</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">thr</span><span style="color: rgb(0, 0, 0);">%</span><span style="color: rgb(0, 0, 0);">20ee#</span><span style="color: rgb(0, 0, 0);">23</span><span style="color: rgb(0, 0, 0);"><br />Decoded: Hello World</span><span style="color: rgb(0, 0, 0);">?</span></div>         <br />参考：<br />        在这个例子中，URLUtil对传入的查询字符串的内容进行了编码。最近，HttpClient小组将一些URL编码和解码的逻辑代码移入了Jakarta Commons Codec项目中，对应的类名为URLCodec。需要URLCodec更多的信息，请参考</span>
				</font>
		</font>
		<font>
				<font face="Georgia">
						<span style="color: rgb(0, 0, 0);">Jakarta Commons Codec项目主页（http://jakarta.apache.org/codec）。</span>
				</font>
		</font>
		<br />
		<font>
				<font face="Georgia">
						<span style="color: rgb(0, 0, 0);">         RFC1738讨论了URL中的合法字符，并规定了对其他字符进行编码的过程。RFC1738能够在http:// www.zvon.org/tmRFC/RFC2616/Output/index.html中找到。 </span>
						<span style="color: rgb(0, 0, 0);">
						</span>
				</font>
		</font>
<img src ="http://www.blogjava.net/guangnian0412/aggbug/39235.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/guangnian0412/" target="_blank">guangnian</a> 2006-04-04 19:19 <a href="http://www.blogjava.net/guangnian0412/archive/2006/04/04/39235.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[HttpClient] 执行HTTP GET方法  （from Jakarta Commons Cookbook 11。3）</title><link>http://www.blogjava.net/guangnian0412/archive/2006/04/04/39044.html</link><dc:creator>guangnian</dc:creator><author>guangnian</author><pubDate>Mon, 03 Apr 2006 16:18:00 GMT</pubDate><guid>http://www.blogjava.net/guangnian0412/archive/2006/04/04/39044.html</guid><wfw:comment>http://www.blogjava.net/guangnian0412/comments/39044.html</wfw:comment><comments>http://www.blogjava.net/guangnian0412/archive/2006/04/04/39044.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/guangnian0412/comments/commentRss/39044.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/guangnian0412/services/trackbacks/39044.html</trackback:ping><description><![CDATA[
		<font face="Georgia">问题：<br />        你需要通过HTTP GET方法来获取信息。<br /><br />解答：<br />        创建一个HttpClient实例，并调用以GetMethod对象为参数的executeMethod方法。然后，响应的内容就可以通过一个InputStream，byte[]，或者是String来获得。下面的例子将获得
<a href="http://www.discursive.com/jccook/">http://www.discursive.com /jccook/</a>的内容，并且以一个String来获得响应。<br />        <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);"> 1</span> <span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.apache.commons.httpclient.HttpClient;<br /></span><span style="color: rgb(0, 128, 128);"> 2</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.apache.commons.httpclient.HttpException;<br /></span><span style="color: rgb(0, 128, 128);"> 3</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.apache.commons.httpclient.HttpMethod;<br /></span><span style="color: rgb(0, 128, 128);"> 4</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> org.apache.commons.httpclient.methods.GetMethod;<br /></span><span style="color: rgb(0, 128, 128);"> 5</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);"> 6</span> <span style="color: rgb(0, 0, 0);">HttpClient client </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> HttpClient( );<br /></span><span style="color: rgb(0, 128, 128);"> 7</span> <span style="color: rgb(0, 0, 0);">String url </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">http://www.discursive.com/jccook/</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br /></span><span style="color: rgb(0, 128, 128);"> 8</span> <span style="color: rgb(0, 0, 0);">HttpMethod method </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> GetMethod( url );<br /></span><span style="color: rgb(0, 128, 128);"> 9</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">10</span> <span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 0, 255);">try</span><span style="color: rgb(0, 0, 0);"> {<br /></span><span style="color: rgb(0, 128, 128);">11</span> <span style="color: rgb(0, 0, 0);">    client.executeMethod( method );<br /></span><span style="color: rgb(0, 128, 128);">12</span> <span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 128, 128);">13</span> <span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">( method.getStatusCode( ) </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> HttpStatus.SC_OK ) {<br /></span><span style="color: rgb(0, 128, 128);">14</span> <span style="color: rgb(0, 0, 0);">        String response </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> method.getResponseBodyAsString( );<br /></span><span style="color: rgb(0, 128, 128);">15</span> <span style="color: rgb(0, 0, 0);">        System.out.println( response );<br /></span><span style="color: rgb(0, 128, 128);">16</span> <span style="color: rgb(0, 0, 0);">    }<br /></span><span style="color: rgb(0, 128, 128);">17</span> <span style="color: rgb(0, 0, 0);">} </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);">( HttpException he ) {<br /></span><span style="color: rgb(0, 128, 128);">18</span> <span style="color: rgb(0, 0, 0);">    System.out.println( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">HTTP Problem: </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> he.getMessage( ) );<br /></span><span style="color: rgb(0, 128, 128);">19</span> <span style="color: rgb(0, 0, 0);">} </span><span style="color: rgb(0, 0, 255);">catch</span><span style="color: rgb(0, 0, 0);">( IOException ioe ) {<br /></span><span style="color: rgb(0, 128, 128);">20</span> <span style="color: rgb(0, 0, 0);">    System.out.println( </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">IO Exeception: </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> ioe.getMessage( ) );<br /></span><span style="color: rgb(0, 128, 128);">21</span> <span style="color: rgb(0, 0, 0);">} </span><span style="color: rgb(0, 0, 255);">finally</span><span style="color: rgb(0, 0, 0);"> {<br /></span><span style="color: rgb(0, 128, 128);">22</span> <span style="color: rgb(0, 0, 0);">    method.releaseConnection( );<br /></span><span style="color: rgb(0, 128, 128);">23</span> <span style="color: rgb(0, 0, 0);">    method.recycle( );<br /></span><span style="color: rgb(0, 128, 128);">24</span> <span style="color: rgb(0, 0, 0);">}</span><span style="color: rgb(0, 128, 128);"></span><span style="color: rgb(0, 0, 0);"></span></div>     这段代码用HTTP GET方法获得了</font>
		<font face="Georgia">
				<a href="http://www.discursive.com/jccook/">http://www.discursive.com/jccook/</a>的内容。如果响应的状态码是HttpStatus.SC_OK(即200），下列响应将被输出到控制台：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">html</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">head</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />  </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">title</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">JCCook Example</span><span style="color: rgb(0, 0, 0);">&lt;/</span><span style="color: rgb(0, 0, 0);">title</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">&lt;/</span><span style="color: rgb(0, 0, 0);">head</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">body</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br />  </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">h1</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);">Hello World</span><span style="color: rgb(0, 0, 0);">!&lt;/</span><span style="color: rgb(0, 0, 0);">h1</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">&lt;/</span><span style="color: rgb(0, 0, 0);">body</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 0);">&lt;/</span><span style="color: rgb(0, 0, 0);">html</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br /></span></div><br />讨论：<br />        注意这段代码中对异常的处理。执行一个简单的HTTP GET需要捕捉两个异常：HttpException和IOException。如果是发生HTTP协议错误时，将抛出HttpException异常;如果是发生有关网络的错误时，将抛出IOException异常。这一章后面的例子将会忽略对异常的处理。你应该要知道每一次调用executeMethod()都要用适当的try/catch块包裹。<br />        GetMethod类是HttpMethod接口的一种实现。HttpMethod会被HttpClient所调用。HttpMethod实现类的生命周期是很简单的：一个HttpMethod实例被创建，然后被HttpClient调用；一旦响应被检测到以后，HttpMethod释放连接并被回收使用。当HttpMethod调用了recycle()方法，相当于发送了一个信号给系统表示这个HttpMethod实例可以再被使用。releaseConnection()方法指示HttpClient释放掉与这个HttpMethod相关联的连接。无论在使用HttpMethod实例的过程中发生了什么，都要调用releaseConnection()来释放网络资源。<br />        一旦HttpClient的executeMethod方法被调用，你可以通过HttpMethod的getStatusCode()方法来获得响应的状态码。这个方法返回一个int，对应于HttpStatus类的public static final 变量。HttpStatus类还包括下面一些常量：SC_OK(200)，SC_NOT_FOUND(404)，SC_INTERNAL_SERVER_ERROR(500)，SC_MOVED_TEMPORARILY (302)，SC_UNAUTHORIZED(401)等等。请参照HttpStatus的Javadoc来获得所有的HTTP状态列表。当服务器返回一个错误的HTTP状态是，通常还会返回一小段信息。这一小段信息可以通过HttpMethod类的getStatusText()方法获得。<br /> <br />参考：<br />       可以从RFC2616（http://www.zvon.org/tmRFC/RFC2616/Output/index.html)获得HTTP GET方法的官方定义；<br />       要获得HTTP 状态码的完整列表，请参见 HttpStatus Javadoc  (http://jakarta.apache.org/commons/ httpclient/apidocs/index.html）。<br /></font>
<img src ="http://www.blogjava.net/guangnian0412/aggbug/39044.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/guangnian0412/" target="_blank">guangnian</a> 2006-04-04 00:18 <a href="http://www.blogjava.net/guangnian0412/archive/2006/04/04/39044.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Jakarta Commons Cookbook</title><link>http://www.blogjava.net/guangnian0412/archive/2006/04/03/39036.html</link><dc:creator>guangnian</dc:creator><author>guangnian</author><pubDate>Mon, 03 Apr 2006 15:25:00 GMT</pubDate><guid>http://www.blogjava.net/guangnian0412/archive/2006/04/03/39036.html</guid><wfw:comment>http://www.blogjava.net/guangnian0412/comments/39036.html</wfw:comment><comments>http://www.blogjava.net/guangnian0412/archive/2006/04/03/39036.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.blogjava.net/guangnian0412/comments/commentRss/39036.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/guangnian0412/services/trackbacks/39036.html</trackback:ping><description><![CDATA[    <font face="Georgia">其实Jakarta Commons类库在Java中的地位是很重要的，它为Java程序员提供了很多可重用的工具。最近在看Jakarta Commons Cookbook 这本书，是英文版，忽然有想翻译它的冲动。就从我最近用到的HttpClient开始吧.......</font><img src ="http://www.blogjava.net/guangnian0412/aggbug/39036.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/guangnian0412/" target="_blank">guangnian</a> 2006-04-03 23:25 <a href="http://www.blogjava.net/guangnian0412/archive/2006/04/03/39036.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>