??xml version="1.0" encoding="utf-8" standalone="yes"?>国产一区二区三区综合,欧美精品在线免费观看,亚洲一区二区三区四区五区午夜http://www.aygfsteel.com/guangnian0412/category/9444.htmlJava in my lifezh-cnTue, 27 Feb 2007 10:25:00 GMTTue, 27 Feb 2007 10:25:00 GMT60[Commons Logging]使用一个抽象的Logging接口QFrom Jarkata Commons Cookbook 7.10Q?/title><link>http://www.aygfsteel.com/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.aygfsteel.com/guangnian0412/archive/2006/04/24/42874.html</guid><wfw:comment>http://www.aygfsteel.com/guangnian0412/comments/42874.html</wfw:comment><comments>http://www.aygfsteel.com/guangnian0412/archive/2006/04/24/42874.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/guangnian0412/comments/commentRss/42874.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/guangnian0412/services/trackbacks/42874.html</trackback:ping><description><![CDATA[ <font face="Georgia">   QJarkata ?Commons Logging 包现在已l被用在几乎所有的开源项目之中,它可以你开发的pȝ工作在不同的日志框架下,包括Sun的logging框架和Apache Log4j。现在Commons Logging + Apache Log4j 的n影是随处可见QCommons Logging 的易用与Log4j的强大功能Ş成了l配。)<br /><br />问题Q?br />      你正在写一个可重用的代码库Q而你不知道你的代码在哪里q且是如何工作的。你需要一个抽象的日志接口来写入日志信息,因ؓ你不能确定Log4j或者是JDK 1.4 logging的存在性?br /><br />解决Q?br />        通过Jakarta Commons Logging 的Log 接口来记录信息,然后依靠Commons Logging自n来决定在q行时用哪U具体的日志框架。下面的代码使用了Log接口来记录traceQdebugQinfoQwarningQerror和fatal信息Q?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() q回一个Log接口的具体实玎ͼq个实现与底层具体的日志框架相对应。例如,如果你的pȝ是用Apache Log4j Q一个Log4JLogger被q回Q对应于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 />讨论Q?br />        一个可重用代码库的开发者不能预知其代码库将在何时何地被用到Q而现在有很多的日志框架可以用,所以当开发可重用代码库的时候,使用Commons Logging 是非常明智的Q例如Jakarta Commons lg。当调用LogFactory.getInstance()Ҏ的时候,Commons Logging 通过pȝ属性和classpath中的cd来决定和理适当的日志框架。对于一个小型可重用lg的开发者来_q行日志记录只需要调用Log接口。而配|底层日志框架的负担Q就转移C用其lg库的开发者n上?br /><br />参考:<br />        7.11节详l的说明了Commons Logging在运行时定适当日志框架的算法?br /><br /></span> </font> </font> <font face="Georgia">     <br /></font> <img src ="http://www.aygfsteel.com/guangnian0412/aggbug/42874.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/guangnian0412/" target="_blank">guangnian</a> 2006-04-24 18:32 <a href="http://www.aygfsteel.com/guangnian0412/archive/2006/04/24/42874.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[HttpClient] 创徏一个HTTP POST h (from Jakarta Commons cookbook 11.7 )http://www.aygfsteel.com/guangnian0412/archive/2006/04/11/40529.htmlguangnianguangnianTue, 11 Apr 2006 14:41:00 GMThttp://www.aygfsteel.com/guangnian0412/archive/2006/04/11/40529.htmlhttp://www.aygfsteel.com/guangnian0412/comments/40529.htmlhttp://www.aygfsteel.com/guangnian0412/archive/2006/04/11/40529.html#Feedback2http://www.aygfsteel.com/guangnian0412/comments/commentRss/40529.htmlhttp://www.aygfsteel.com/guangnian0412/services/trackbacks/40529.html问题Q?br />        你需要用HTTP POST Ҏ来向一个servlet传递参数?br />
讨论Q?br />        创徏一?PostMethod 对象Q然后调?setParameter() ?addParameter() Ҏ讄参数?PostMethod 对象会传送一?Content-Type 头ؓ application/x-www-form-urlencoded 的请求,q且参数在hbody中被传送。在下列的例子中演示了用 PostMethod 对象传递参数的用法Q?br />
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

HttpClient client 
= new HttpClient( );

// Create POST method
String url = "http://www.discursive.com/cgi-bin/jccook/param_list.cgi";
PostMethod method 
= new PostMethod( url );

// Set parameters on POST    
method.setParameter( "test1""Hello World" );
method.addParameter( 
"test2""This is a Form Submission" );
method.addParameter( 
"Blah""Whoop" );
method.addParameter( 
new NameValuePair( "Blah""Whoop2" ) );

// Execute and print response
client.executeMethod( method );
String response 
= method.getResponseBodyAsString( );
System.out.println( response );

method.releaseConnection( );
       param_list.cgi CGI脚本会对所以接收到的参数进行回显,从下面的输出中,你可以看C递给CGI脚本的三个参敎ͼ
These are the parameters I received:

test1:
  Hello World
test2:
  This is a Form Submission
Blah:
  Whoop
  Whoop2
       有几U方法来在一个PostMethod对象中设|参数。最直接的方法就是调用setParameter()ҎQƈ传递两个字W串l它Q参数的名称和参数倹{setParameter()Ҏ会替代M已经存在的同名参数。但是,如果一个同名的参数已经存在一个PostMethod对象中,addParameter()会加入另一个同名参数|addParameter()Ҏ同样接受两个StringQ参数名和参数倹{另一U方法,q两个方法同h受一个包装了参数名和参数值的NameValuePair对象。在前面的例子中Q通过addParameter()ҎQ用参数名Blah传递了两个|W一ơ用两个String作ؓ参数Q第二次用一个NameValuePair对象作ؓ参数?br />


guangnian 2006-04-11 22:41 发表评论
]]>
[HttpClient] 在查询字W串中传送参?Qfrom Jakarta Commons Cookbook 11?Q?/title><link>http://www.aygfsteel.com/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.aygfsteel.com/guangnian0412/archive/2006/04/04/39235.html</guid><wfw:comment>http://www.aygfsteel.com/guangnian0412/comments/39235.html</wfw:comment><comments>http://www.aygfsteel.com/guangnian0412/archive/2006/04/04/39235.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/guangnian0412/comments/commentRss/39235.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/guangnian0412/services/trackbacks/39235.html</trackback:ping><description><![CDATA[ <font face="Georgia">问题Q?br />        你需要在一个URL中传送查询参数?br /><br />解答Q?br />        使用一个HttpMethod实例的setQueryString()Ҏ来设|查询字W串。用URIUtilcd包含在URL中的文本q行~码。下面的例子在查询字W串中放入了两个参数Q?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()来设|查询字W串</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&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);">&</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>      提示Q你不必在setQueryString()Ҏ中加入?P当HttpClient实例执行executeMethod()ҎӞ它会被自动加入?br /><br />讨论Q?br />         在前面的例子中,HttpMethod的setQueryString()Ҏ是一ơ性将整个查询字符串加q去Q但是还有另外一U选择Q通过一个NameValuePair对象的数l来讄查询字符丌Ӏ当一个NameValuePair[]传入setQueryString()Ҏ中时QHttpMethod实例会从数组中取出每一个NameValuePair对象Q然后创Zpd?amp;号分割的参数。这U方法ɽE序代码更加q净Q因Z不必q接字符串来传递多个参数。下面的例子用NameValuePair对象Q与前一个例子设|了同样的参敎ͼ<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>        ҎRFC1738QURL只能够包含字母和数字字符Q[0-9,a-z,A-Z]和一些特D字W。如果你需要在参数中传送一些URL所不允许的字符Q你需要对你的字符串进行编码,以符合RFC1738的规定。URIUtilcL一个方法encodeQuery()能够对前面例子中?O Reilly"q行~码。下面的代码展示了用URIUtilcL对包含在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);"><test>=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 />        q个单的例子用URIUtilcd两个字符串进行了~码Qƈ对一个经q编码的字符串进行解码。下面的输出展示了每个{换的l果Q?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 />        在这个例子中QURLUtil对传入的查询字符串的内容q行了编码。最q,HttpClient组一些URL~码和解码的逻辑代码Ud了Jakarta Commons Codec目中,对应的类名ؓURLCodec。需要URLCodec更多的信息,请参?/span> </font> </font> <font> <font face="Georgia"> <span style="color: rgb(0, 0, 0);">Jakarta Commons Codec目主页Qhttp://jakarta.apache.org/codecQ?/span> </font> </font> <br /> <font> <font face="Georgia"> <span style="color: rgb(0, 0, 0);">         RFC1738讨论了URL中的合法字符Qƈ规定了对其他字符q行~码的过E。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.aygfsteel.com/guangnian0412/aggbug/39235.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/guangnian0412/" target="_blank">guangnian</a> 2006-04-04 19:19 <a href="http://www.aygfsteel.com/guangnian0412/archive/2006/04/04/39235.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[HttpClient] 执行HTTP GETҎ Qfrom Jakarta Commons Cookbook 11?Q?/title><link>http://www.aygfsteel.com/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.aygfsteel.com/guangnian0412/archive/2006/04/04/39044.html</guid><wfw:comment>http://www.aygfsteel.com/guangnian0412/comments/39044.html</wfw:comment><comments>http://www.aygfsteel.com/guangnian0412/archive/2006/04/04/39044.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/guangnian0412/comments/commentRss/39044.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/guangnian0412/services/trackbacks/39044.html</trackback:ping><description><![CDATA[ <font face="Georgia">问题Q?br />        你需要通过HTTP GETҎ来获取信息?br /><br />解答Q?br />        创徏一个HttpClient实例Qƈ调用以GetMethod对象为参数的executeMethodҎ。然后,响应的内容就可以通过一个InputStreamQbyte[]Q或者是String来获得。下面的例子获? <a >http://www.discursive.com /jccook/</a>的内容,q且以一个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>     q段代码用HTTP GETҎ获得?/font> <font face="Georgia"> <a >http://www.discursive.com/jccook/</a>的内宏V如果响应的状态码是HttpStatus.SC_OK(?00Q,下列响应被输出到控制台Q?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);"><</span><span style="color: rgb(0, 0, 0);">html</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">head</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br />  </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">title</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);">JCCook Example</span><span style="color: rgb(0, 0, 0);"></</span><span style="color: rgb(0, 0, 0);">title</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 0);"></</span><span style="color: rgb(0, 0, 0);">head</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">body</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br />  </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">h1</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);">h1</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 0);"></</span><span style="color: rgb(0, 0, 0);">body</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 0);"></</span><span style="color: rgb(0, 0, 0);">html</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /><br /></span></div><br />讨论Q?br />        注意q段代码中对异常的处理。执行一个简单的HTTP GET需要捕捉两个异常:HttpException和IOException。如果是发生HTTP协议错误Ӟ抛出HttpException异常;如果是发生有关网l的错误Ӟ抛出IOException异常。这一章后面的例子会忽略对异常的处理。你应该要知道每一ơ调用executeMethod()都要用适当的try/catch块包裏V?br />        GetMethodcLHttpMethod接口的一U实现。HttpMethod会被HttpClient所调用。HttpMethod实现cȝ生命周期是很单的Q一个HttpMethod实例被创建,然后被HttpClient调用Q一旦响应被到以后QHttpMethod释放q接q被回收使用。当HttpMethod调用了recycle()ҎQ相当于发送了一个信Lpȝ表示q个HttpMethod实例可以再被使用。releaseConnection()Ҏ指示HttpClient释放掉与q个HttpMethod相关联的q接。无论在使用HttpMethod实例的过E中发生了什么,都要调用releaseConnection()来释攄l资源?br />        一旦HttpClient的executeMethodҎ被调用,你可以通过HttpMethod的getStatusCode()Ҏ来获得响应的状态码。这个方法返回一个intQ对应于HttpStatuscȝpublic static final 变量。HttpStatusc还包括下面一些常量:SC_OK(200)QSC_NOT_FOUND(404)QSC_INTERNAL_SERVER_ERROR(500)QSC_MOVED_TEMPORARILY (302)QSC_UNAUTHORIZED(401){等。请参照HttpStatus的Javadoc来获得所有的HTTP状态列表。当服务器返回一个错误的HTTP状态是Q通常q会q回一段信息。这一段信息可以通过HttpMethodcȝgetStatusText()Ҏ获得?br /> <br />参考:<br />       可以从RFC2616Qhttp://www.zvon.org/tmRFC/RFC2616/Output/index.html)获得HTTP GETҎ的官方定义;<br />       要获得HTTP 状态码的完整列表,请参?HttpStatus Javadoc  (http://jakarta.apache.org/commons/ httpclient/apidocs/index.htmlQ?br /></font> <img src ="http://www.aygfsteel.com/guangnian0412/aggbug/39044.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/guangnian0412/" target="_blank">guangnian</a> 2006-04-04 00:18 <a href="http://www.aygfsteel.com/guangnian0412/archive/2006/04/04/39044.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Jakarta Commons Cookbookhttp://www.aygfsteel.com/guangnian0412/archive/2006/04/03/39036.htmlguangnianguangnianMon, 03 Apr 2006 15:25:00 GMThttp://www.aygfsteel.com/guangnian0412/archive/2006/04/03/39036.htmlhttp://www.aygfsteel.com/guangnian0412/comments/39036.htmlhttp://www.aygfsteel.com/guangnian0412/archive/2006/04/03/39036.html#Feedback2http://www.aygfsteel.com/guangnian0412/comments/commentRss/39036.htmlhttp://www.aygfsteel.com/guangnian0412/services/trackbacks/39036.html其实Jakarta Commonscd在Java中的C是很重要的,它ؓJavaE序员提供了很多可重用的工具。最q在看Jakarta Commons Cookbook q本书,是英文版Q忽然有想翻译它的冲动。就从我最q用到的HttpClient开始吧.......

guangnian 2006-04-03 23:25 发表评论
]]>
վ֩ģ壺 | | | | | | Ǭ| »| ƽ̶| | ƽ| ɽ| ٳ| | ƽ| | | | н| лͨ| | | | | ͼľ| »Ȱ| ض| ͷ| | | ˴| ̩| | ƽ| | | ڶ| | | Դ| Ž|