﻿<?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>IT博客网-快乐着飞舞着 &lt;script&gt;_time_stamp = (new Date()).getTime(); _site = 3733;&lt;/script&gt;-随笔分类-Java</title><link>http://www.cnitblog.com/reene/category/899.html</link><description>---Nothing to do  is  doing nothing.&lt;script language='javascript' src='http://stat.gaya.cn/file/stat_client.js'&gt;&lt;/script&gt;</description><language>zh-cn</language><lastBuildDate>Thu, 08 Mar 2007 02:49:00 GMT</lastBuildDate><pubDate>Thu, 08 Mar 2007 02:49:00 GMT</pubDate><ttl>60</ttl><item><title>Spring + hibernate 的错误 </title><link>http://www.cnitblog.com/reene/archive/2006/08/08/14952.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Tue, 08 Aug 2006 11:00:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2006/08/08/14952.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/14952.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2006/08/08/14952.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/14952.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/14952.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 18				:				55				:				43				,				750				 ERROR [ContextLoaderPlugIn] Context initialization failedorg.springframework.beans.factory.BeanCreationException: Error creating bean with name ...&nbsp;&nbsp;<a href='http://www.cnitblog.com/reene/archive/2006/08/08/14952.html'>阅读全文</a><img src ="http://www.cnitblog.com/reene/aggbug/14952.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2006-08-08 19:00 <a href="http://www.cnitblog.com/reene/archive/2006/08/08/14952.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一个诡异问题终于得已解决</title><link>http://www.cnitblog.com/reene/archive/2006/08/08/14949.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Tue, 08 Aug 2006 09:49:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2006/08/08/14949.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/14949.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2006/08/08/14949.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/14949.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/14949.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 最近在学着在 struts 中使用 spring ,但是在发布到服务器上时总是有如表一的错误 。所有的配置文件 都是正确的，在网上就根本就没有什么解决办法。下载一个例子能发布成功，配置文件基本一致，我仔细比较了两个文件 发现差别就是 发布到服务器上 的war文件是否在/WEB-INF/lib文件夹下包含了struts 的所有类库，我当时觉得问题就是出在这里。把所有的依赖类库复制到jboss ser...&nbsp;&nbsp;<a href='http://www.cnitblog.com/reene/archive/2006/08/08/14949.html'>阅读全文</a><img src ="http://www.cnitblog.com/reene/aggbug/14949.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2006-08-08 17:49 <a href="http://www.cnitblog.com/reene/archive/2006/08/08/14949.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate 的 主键生成</title><link>http://www.cnitblog.com/reene/archive/2006/08/05/14782.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sat, 05 Aug 2006 13:51:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2006/08/05/14782.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/14782.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2006/08/05/14782.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/14782.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/14782.html</trackback:ping><description><![CDATA[Hibernate 的主键生成 中的 uuid.hex本来为什么不输出long 类型的 而要输出一个string 类型的呢？ 总觉得一个varchar2 作为主键怪怪的 。。。<br /><img src ="http://www.cnitblog.com/reene/aggbug/14782.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2006-08-05 21:51 <a href="http://www.cnitblog.com/reene/archive/2006/08/05/14782.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Hibernate 的错误 </title><link>http://www.cnitblog.com/reene/archive/2006/07/23/14094.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sun, 23 Jul 2006 15:21:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2006/07/23/14094.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/14094.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2006/07/23/14094.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/14094.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/14094.html</trackback:ping><description><![CDATA[2006-07-23 23:14:53,796 [main] WARN  org.hibernate.impl.SessionFactoryObjectFactory - Could not bind factory to JNDI<br />javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial<br /> at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)<br /> at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)<br /> at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:284)<br /> at javax.naming.InitialContext.getNameParser(InitialContext.java:439)<br /> at org.hibernate.util.NamingHelper.bind(NamingHelper.java:52)<br /> at org.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:90)<br /> at org.hibernate.impl.SessionFactoryImpl.&lt;init&gt;(SessionFactoryImpl.java:291)<br /> at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1176)<br /> at reene.study.hibernate.test.TestMain.main(TestMain.java:19)<br /><br /><br />疑惑中<br /><br />待解决....<br />在网上找到如下内容：<br />（3）报错：Could not bind factory to JNDI<br />此错误是配置文件的问题，如果hibernate配置文件有session_factory_name这个变量，&lt;session-factory name="foo"&gt; 会试图将一个SessionFactory实例以foo为名bind到jndi上，而有的application container不支持jndi绑定。把这个变量去掉即可。<br /><br />解决问题 <br /><br />把引用代码也要修改：<br />大致改成这样：<br /><br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080">1</span> <span style="COLOR: #000000">SessionFactory sf </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> Configuration().configure()<br /></span><span style="COLOR: #008080">2</span> <span style="COLOR: #000000">                    .buildSessionFactory();<br /></span><span style="COLOR: #008080">3</span> <span style="COLOR: #000000">            Session session </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> sf.openSession();<br /></span><span style="COLOR: #008080">4</span> <span style="COLOR: #000000">            Transaction tx </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> session.beginTransaction();</span></div><br /><br /><br /><img src ="http://www.cnitblog.com/reene/aggbug/14094.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2006-07-23 23:21 <a href="http://www.cnitblog.com/reene/archive/2006/07/23/14094.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>"struts中文问题"，"struts国际化问题"??终极解决方案[转载]</title><link>http://www.cnitblog.com/reene/archive/2006/06/13/12193.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Tue, 13 Jun 2006 13:32:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2006/06/13/12193.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/12193.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2006/06/13/12193.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/12193.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/12193.html</trackback:ping><description><![CDATA[<table class="fixedTable blogpost" cellspacing="0" width="100%" border="0">
				<tbody>
						<tr>
								<td class="ellipse">
										<span class="bvTitle" id="subjcns!F0C1403231EB3BB0!151">
												<strong>引用 自：<br /><a href="http://hellocoffeecat.spaces.msn.com/Blog/cns!F0C1403231EB3BB0!151.entry">http://hellocoffeecat.spaces.msn.com/Blog/cns!F0C1403231EB3BB0!151.entry</a><br /><a href="http://hellocoffeecat.spaces.msn.com/blog/cns!F0C1403231EB3BB0!151.trak">http://hellocoffeecat.spaces.msn.com/blog/cns!F0C1403231EB3BB0!151.trak</a><br /><br /><br />"struts中文问题"，"struts国际化问题"??终极解决方案</strong>
										</span>
								</td>
						</tr>
						<tr>
								<td class="bvh8">
										<strong>
										</strong>
								</td>
						</tr>
						<tr>
								<td id="msgcns!F0C1403231EB3BB0!151">
										<div>Java本身就支持多国语言编码，不需要写任何程序，可以很简单的 <br />实现。 <br />秘诀就是两点： <br /><br />1、所有HTML/JSP页面全部采用UTF-8编码 <br /><br />2、客户端浏览器完全支持UTF-8编码 <br /><br />步骤： <br />1、首先把所有的HTML/JSP的ContentType都设为UTF-8 <br /><br />2、然后对于JSP程序中的非ASCII码提示信息都不应该写在程序里面，都应该放在 <br />application.properties里面统一管理。 <br /><br />3、对HTML用native2ascii工具统一做一次处理，把HTML中的非ASCII码都转换为Unicode编码。 <br /><br />4、针对不同的语言，写不同的application.properties，比如说简体中文是 <br />application_zh_CN.properties，繁体中文是application_zh_TW.properties这样，然后对这些配置信 <br />息文件同样用native2ascii工具处理一次，把非ASCII码统统转为Unicode编码。 <br /><br />5、在Servlet的request.getCharacterEncoding()获得客户端的操作系统默认编码，然后set到Struts <br />的HTTPSession的Locale中。 <br /><br />OK！现在不同的客户访问，就会显示不同的语言版本了。你可以看看此时你的浏览器的字符集，就是 <br />UTF-8。现在你的网站和Google一样了，嘿嘿，其实你有心的话，看看你的浏览器访问Google的时候是 <br />什么字符集吧 <br /><br />切记：所有的HTML/JSP都要设为UTF-8编码，所有的文件中的非ASCII码字符都要用native2ascii工具转 <br />为用ASCII表示的Unicode编码。 <br />－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－<br />－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－<br />原创<br />－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－<br />上面所述是我从网上下的一篇于中文问题的解决方案，确切的说应该是关于Struts的国际化问题，下面我结合我的实践谈谈具体如何实现Struts的国际化问题，我对理论不是非常精通，我只能完全凭自己的理解和实践来讲述，所以下面讲的内容可能不是非常正确，还请大家原谅。但有一点可以肯定，我通过自己的努力解决了Struts的中文问题，并实现Struts的国际化，其实一切并不复杂，下面是具体步骤：<br /><br />0.遇到的问题(这些问题也许不会同时出现)<br />a.中文数据从数据库中到jsp中后就变成了"????"<br />b.做好的中文properties文件，其中的中文value在页面显示乱码<br />c.jsp文件中的中文到浏览器后显示时也是乱码（建议不要在jsp文件中输入中文，尽量放在properties文件中）<br />d.由jsp传给bean的中文值，再由bean传回页面又是乱码<br />e.当更换本地浏览器的语言选项时，Web应用程序不能自动根据你的locale选择合适的*.properties文件。导致Web应用程序不能国际化。<br />1.环境：<br />Web服务器： Tomcat 5.0.19<br />操作系统： Win2000 Server<br />JVM ： jdk 1.4<br />数 据 库： Oracle 8.1.7<br />开发工具： struts studio 5.2 pro for eclipse<br />2.先将所有*.jsp 网页中开头处加入<br /><br />&lt;%@ page language="java" contentType="text/html; charset=utf-8" %&gt;<br />再设置&lt;html:html locale = "true"&gt;<br /><br />3.然后编辑好两个*.properties文件，放在classes文件夹下你指定的地方，这里是放在/web-inf/classes/com/wiley 下，它们分别是：<br /><br />ApplicationResources.properties (英文资源文件)<br />ApplicationResources_zh.properties (中文资源文件)<br />随便用什么工具编写都行啊！<br />4.将ApplicationResources_zh.properties转码成gb2312。上面引文说要转成UTF-8，结果我试了，不行。转成gb2312就行了，操作是。<br />将ApplicationResources_zh.properties更名为ApplicationResources_xx.properties<br />在DOS命令行进入ApplicationResources_xx.properties所在的文件夹<br />使用命令：native2ascii -encoding gb2312 ApplicationResources_xx.properties ApplicationResources_zh.properties(至于你为什么会出现“native2ascii不是内部命令”，，请查其它资料，可能你要设置环境变量，因为他是jdk的文件夹bin下的一个应用程序)<br />5.接下来配置struts-config.xml,很简单，我们加入：<br /><br />&lt;message-resources parameter="com.wiley.ApplicationResources"/&gt; 就行了；<br /><br />到此已能解决大多数中文问题。如上面所说的a,b,e 现在打开浏览器，选择菜单：工具》internet选项》语言，将“中文－中国[zh-cn]”删掉，添加一个“英语－英国[zh-gb]”确定后，重启Tomcat,输入网址你就会发现，你的页面的文本信息就会用的是ApplicationResources.properties (英文资源文件)中的内容。如果换回“中文－中国[zh-cn]”，它就会显示ApplicationResources_zh.properties (中文资源文件)中的中文内容。<br /><br />至于问题“c.jsp文件中的中文到浏览器后显示时也是乱码” 你就要用与第4步类似的方法来重新对*.jsp 文件编码，这时-encoding的参数就要用UTF-8了，如果你用的也是struts studio 5.2 pro for eclipse工具，这一步就免了。它会自动用UTF-8的格式存储。<br />至于问题“d.由jsp传给bean的中文值，再由bean传回页面又是乱码”的解决，我只是加了个过滤器。<br />你可以现在web.xml中加入：<br />&lt;filter&gt;<br />&lt;filter-name&gt;Set Character Encoding&lt;/filter-name&gt;<br />&lt;filter-class&gt;com.wiley.SetCharacterEncodingFilter&lt;/filter-class&gt;<br />&lt;init-param&gt;<br />&lt;param-name&gt;encoding&lt;/param-name&gt;<br />&lt;param-value&gt;utf-8&lt;/param-value&gt; <br />&lt;/init-param&gt;<br />&lt;init-param&gt;<br />&lt;param-name&gt;ignore&lt;/param-name&gt;<br />&lt;param-value&gt;true&lt;/param-value&gt;<br />&lt;/init-param&gt;<br />&lt;/filter&gt;<br />&lt;filter-mapping&gt;<br />&lt;filter-name&gt;Set Character Encoding&lt;/filter-name&gt;<br />&lt;servlet-name&gt;action&lt;/servlet-name&gt;<br />&lt;/filter-mapping&gt;<br /><br />然后在你指定的包内加个java文件 我放在了/web-inf/classes/com/wiley 里，下面是源代码：<br /><br /><br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008000">/*</span><span style="COLOR: #008000"><br />* XP Forum<br />* <br />* Copyright (c) 2002-2003 RedSoft Group. All rights reserved.<br />*<br /></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">package</span><span style="COLOR: #000000"> com.huahang.tj.struts.filters;<br /><br /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> javax.servlet.</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> java.io.IOException;<br /><br /></span><span style="COLOR: #008000">/**</span><span style="COLOR: #008000"><br />* &lt;p&gt;Filter that sets the character encoding to be used in parsing the<br />* incoming request, either unconditionally or only if the client did not<br />* specify a character encoding. Configuration of this filter is based on<br />* the following initialization parameters:&lt;/p&gt;<br />* &lt;ul&gt;<br />* &lt;li&gt;&lt;strong&gt;encoding&lt;/strong&gt; - The character encoding to be configured<br />* for this request, either conditionally or unconditionally based on<br />* the &lt;code&gt;ignore&lt;/code&gt; initialization parameter. This parameter<br />* is required, so there is no default.&lt;/li&gt;<br />* &lt;li&gt;&lt;strong&gt;ignore&lt;/strong&gt; - If set to "true", any character encoding<br />* specified by the client is ignored, and the value returned by the<br />* &lt;code&gt;selectEncoding()&lt;/code&gt; method is set. If set to "false,<br />* &lt;code&gt;selectEncoding()&lt;/code&gt; is called &lt;strong&gt;only&lt;/strong&gt; if the<br />* client has not already specified an encoding. By default, this<br />* parameter is set to "true".&lt;/li&gt;<br />* &lt;/ul&gt;<br />*<br />* &lt;p&gt;Although this filter can be used unchanged, it is also easy to<br />* subclass it and make the &lt;code&gt;selectEncoding()&lt;/code&gt; method more<br />* intelligent about what encoding to choose, based on characteristics of<br />* the incoming request (such as the values of the &lt;code&gt;Accept-Language&lt;/code&gt;<br />* and &lt;code&gt;User-Agent&lt;/code&gt; headers, or a value stashed in the current<br />* user´s session.&lt;/p&gt;<br />*<br />* </span><span style="COLOR: #808080">@author</span><span style="COLOR: #008000"> &lt;a href="mailto:jwtronics@yahoo.com"&gt;John Wong&lt;/a&gt;<br />*<br />* </span><span style="COLOR: #808080">@version</span><span style="COLOR: #008000"> $Id: SetCharacterEncodingFilter.java,v 1.1 2002/04/10 13:59:27 johnwong Exp $<br /></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> SetCharacterEncodingFilter </span><span style="COLOR: #0000ff">implements</span><span style="COLOR: #000000"> Filter {<br /><br /></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> ----------------------------------------------------- Instance Variables</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000"><br /><br /></span><span style="COLOR: #008000">/**</span><span style="COLOR: #008000"><br />* The default character encoding to set for requests that pass through<br />* this filter.<br /></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">protected</span><span style="COLOR: #000000"> String encoding </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">;<br /><br /><br /></span><span style="COLOR: #008000">/**</span><span style="COLOR: #008000"><br />* The filter configuration object we are associated with. If this value<br />* is null, this filter instance is not currently configured.<br /></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">protected</span><span style="COLOR: #000000"> FilterConfig filterConfig </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">;<br /><br /><br /></span><span style="COLOR: #008000">/**</span><span style="COLOR: #008000"><br />* Should a character encoding specified by the client be ignored?<br /></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">protected</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">boolean</span><span style="COLOR: #000000"> ignore </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br /><br /><br /></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> --------------------------------------------------------- Public Methods</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000"><br /><br /></span><span style="COLOR: #008000">/**</span><span style="COLOR: #008000"><br />* Take this filter out of service.<br /></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> destroy() {<br /><br /></span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.encoding </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.filterConfig </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">;<br /><br />}<br /><br /><br /></span><span style="COLOR: #008000">/**</span><span style="COLOR: #008000"><br />* Select and set (if specified) the character encoding to be used to<br />* interpret request parameters for this request.<br />*<br />* </span><span style="COLOR: #808080">@param</span><span style="COLOR: #008000"> request The servlet request we are processing<br />* </span><span style="COLOR: #808080">@param</span><span style="COLOR: #008000"> result The servlet response we are creating<br />* </span><span style="COLOR: #808080">@param</span><span style="COLOR: #008000"> chain The filter chain we are processing<br />*<br />* </span><span style="COLOR: #808080">@exception</span><span style="COLOR: #008000"> IOException if an input/output error occurs<br />* </span><span style="COLOR: #808080">@exception</span><span style="COLOR: #008000"> ServletException if a servlet error occurs<br /></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> doFilter(ServletRequest request, ServletResponse response,<br />FilterChain chain)<br /></span><span style="COLOR: #0000ff">throws</span><span style="COLOR: #000000"> IOException, ServletException {<br /><br /></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Conditionally select and set the character encoding to be used</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (ignore </span><span style="COLOR: #000000">||</span><span style="COLOR: #000000"> (request.getCharacterEncoding() </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">)) {<br />String encoding </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> selectEncoding(request);<br /></span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (encoding </span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">)<br />request.setCharacterEncoding(encoding);<br />}<br /><br /></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> Pass control on to the next filter</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">chain.doFilter(request, response);<br /><br />}<br /><br /><br /></span><span style="COLOR: #008000">/**</span><span style="COLOR: #008000"><br />* Place this filter into service.<br />*<br />* </span><span style="COLOR: #808080">@param</span><span style="COLOR: #008000"> filterConfig The filter configuration object<br /></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> init(FilterConfig filterConfig) </span><span style="COLOR: #0000ff">throws</span><span style="COLOR: #000000"> ServletException {<br /><br /></span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.filterConfig </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> filterConfig;<br /></span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.encoding </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> filterConfig.getInitParameter(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">encoding</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br />String value </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> filterConfig.getInitParameter(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">ignore</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br /></span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (value </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">)<br /></span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.ignore </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (value.equalsIgnoreCase(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">true</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">))<br /></span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.ignore </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (value.equalsIgnoreCase(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">yes</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">))<br /></span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.ignore </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br /></span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.ignore </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br /><br />}<br /><br /><br /></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000"> ------------------------------------------------------ Protected Methods</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000"><br /><br /></span><span style="COLOR: #008000">/**</span><span style="COLOR: #008000"><br />* Select an appropriate character encoding to be used, based on the<br />* characteristics of the current request and/or filter initialization<br />* parameters. If no character encoding should be set, return<br />* &lt;code&gt;null&lt;/code&gt;.<br />* &lt;p&gt;<br />* The default implementation unconditionally returns the value configured<br />* by the &lt;strong&gt;encoding&lt;/strong&gt; initialization parameter for this<br />* filter.<br />*<br />* </span><span style="COLOR: #808080">@param</span><span style="COLOR: #008000"> request The servlet request we are processing<br /></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">protected</span><span style="COLOR: #000000"> String selectEncoding(ServletRequest request) {<br /><br /></span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> (</span><span style="COLOR: #0000ff">this</span><span style="COLOR: #000000">.encoding);<br /><br />}<br /><br />}</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">EOC<br /></span></div><br />到此我遇到的中文问题已全部得到解决，并从中理解到struts的国际化的深刻含义。<br />我个人觉得struts作为一个功能强大的应用框架，应该早就考虑到它的国际化问题，并在实际应用中不会很复杂，只要我们遵循一些规则，就可以尽情享受struts给我们带来的无穷乐趣。希望以上所述对大家有所帮助。</div>
								</td>
						</tr>
						<tr>
								<td>
										<table cellspacing="0" border="0">
												<tbody>
												</tbody>
										</table>
								</td>
						</tr>
				</tbody>
		</table><img src ="http://www.cnitblog.com/reene/aggbug/12193.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2006-06-13 21:32 <a href="http://www.cnitblog.com/reene/archive/2006/06/13/12193.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>应用HttpClient来对付各种顽固的WEB服务器(3) [转]</title><link>http://www.cnitblog.com/reene/archive/2005/10/09/3173.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sun, 09 Oct 2005 05:07:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/10/09/3173.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/3173.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/10/09/3173.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/3173.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/3173.html</trackback:ping><description><![CDATA[<TABLE style="BORDER-RIGHT: #6595d6 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #6595d6 1px dotted; FONT-SIZE: 12px; BORDER-LEFT: #6595d6 1px dotted; BORDER-BOTTOM: #6595d6 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
<TBODY>
<TR>
<TD style="WORD-WRAP: break-word" bgColor=#e8f4ff>
<P>4． 模拟输入用户名和口令进行登录</P>
<P>本小节应该说是HTTP客户端编程中最常碰见的问题，很多网站的内容都只是对注册用户可见的，这种情况下就必须要求使用正确的用户名和口令登录成功后，方可浏览到想要的页面。因为HTTP协议是无状态的，也就是连接的有效期只限于当前请求，请求内容结束后连接就关闭了。在这种情况下为了保存用户的登录信息必须使用到Cookie机制。以JSP/Servlet为例，当浏览器请求一个JSP或者是Servlet的页面时，应用服务器会返回一个参数，名为jsessionid（因不同应用服务器而异），值是一个较长的唯一字符串的Cookie，这个字符串值也就是当前访问该站点的会话标识。浏览器在每访问该站点的其他页面时候都要带上jsessionid这样的Cookie信息，应用服务器根据读取这个会话标识来获取对应的会话信息。</P>
<P>对于需要用户登录的网站，一般在用户登录成功后会将用户资料保存在服务器的会话中，这样当访问到其他的页面时候，应用服务器根据浏览器送上的Cookie中读取当前请求对应的会话标识以获得对应的会话信息，然后就可以判断用户资料是否存在于会话信息中，如果存在则允许访问页面，否则跳转到登录页面中要求用户输入帐号和口令进行登录。这就是一般使用JSP开发网站在处理用户登录的比较通用的方法。</P>
<P>这样一来，对于HTTP的客户端来讲，如果要访问一个受保护的页面时就必须模拟浏览器所做的工作，首先就是请求登录页面，然后读取Cookie值；再次请求登录页面并加入登录页所需的每个参数；最后就是请求最终所需的页面。当然在除第一次请求外其他的请求都需要附带上Cookie信息以便服务器能判断当前请求是否已经通过验证。说了这么多，可是如果你使用httpclient的话，你甚至连一行代码都无需增加，你只需要先传递登录信息执行登录过程，然后直接访问想要的页面，跟访问一个普通的页面没有任何区别，因为类HttpClient已经帮你做了所有该做的事情了，太棒了！下面的例子实现了这样一个访问的过程。</P>
<P><BR>/*</P>
<P>&nbsp;* Created on 2003-12-7 by Liudong</P>
<P>&nbsp;*/</P>
<P>package http.demo;</P>
<P>&nbsp;</P>
<P>import org.apache.commons.httpclient.*;</P>
<P>import org.apache.commons.httpclient.cookie.*;</P>
<P>import org.apache.commons.httpclient.methods.*;</P>
<P>&nbsp;</P>
<P>/**</P>
<P>&nbsp;* 用来演示登录表单的示例</P>
<P>&nbsp;* @author Liudong</P>
<P>&nbsp;*/</P>
<P>public class FormLoginDemo {</P>
<P>&nbsp;</P>
<P>&nbsp;&nbsp;&nbsp; static final String LOGON_SITE = "localhost";</P>
<P>&nbsp;&nbsp;&nbsp; static final int&nbsp;&nbsp;&nbsp; LOGON_PORT = 8080;</P>
<P>&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp; public static void main(String[] args) throws Exception{</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HttpClient client = new HttpClient();</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //模拟登录页面login.jsp-&gt;main.jsp</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PostMethod post = new PostMethod("/main.jsp");</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NameValuePair name = new NameValuePair("name", "ld");&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NameValuePair pass = new NameValuePair("password", "ld");&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; post.setRequestBody(new NameValuePair[]{name,pass});</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int status = client.executeMethod(post);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(post.getResponseBodyAsString());</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; post.releaseConnection();&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //查看cookie信息</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CookieSpec cookiespec = CookiePolicy.getDefaultSpec();</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cookie[] cookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies());</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (cookies.length == 0) {</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("None");&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; cookies.length; i++) {</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(cookies[i].toString());&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //访问所需的页面main2.jsp</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GetMethod get = new GetMethod("/main2.jsp");</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client.executeMethod(get);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(get.getResponseBodyAsString());</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; get.releaseConnection();</P>
<P>&nbsp;&nbsp;&nbsp; }</P>
<P>}</P>
<P>5． 提交XML格式参数</P>
<P>提交XML格式的参数很简单，仅仅是一个提交时候的ContentType问题，下面的例子演示从文件文件中读取XML信息并提交给服务器的过程，该过程可以用来测试Web服务。</P>
<P>import java.io.File;</P>
<P>import java.io.FileInputStream;</P>
<P>&nbsp;</P>
<P>import org.apache.commons.httpclient.HttpClient;</P>
<P>import org.apache.commons.httpclient.methods.EntityEnclosingMethod;</P>
<P>import org.apache.commons.httpclient.methods.PostMethod;</P>
<P>&nbsp;</P>
<P>/**</P>
<P>&nbsp;* 用来演示提交XML格式数据的例子</P>
<P>&nbsp;*/</P>
<P>public class PostXMLClient {</P>
<P>&nbsp;</P>
<P>&nbsp;&nbsp;&nbsp; public static void main(String[] args) throws Exception {</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; File input = new File(“test.xml”);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PostMethod post = new PostMethod(“http://localhost:8080/httpclient/xml.jsp”);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 设置请求的内容直接从文件中读取</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; post.setRequestBody(new FileInputStream(input));</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (input.length() &lt; Integer.MAX_VALUE) </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; post.setRequestContentLength(input.length());</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; post.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 指定请求内容的类型</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; post.setRequestHeader("Content-type", "text/xml; charset=GBK");</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HttpClient httpclient = new HttpClient(); </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int result = httpclient.executeMethod(post); </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Response status code: " + result);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Response body: ");</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(post.getResponseBodyAsString());</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; post.releaseConnection();</P>
<P>&nbsp;&nbsp;&nbsp; }</P>
<P>}</P>
<P>&nbsp;</P></TD></TR></TBODY></TABLE>	(<a href=http://www.blogcn.com/user28/reene/blog/22216733.html target=_blank>浏览全文</a>&nbsp; <a href=http://www.blogcn.com/blog/trackback.asp?mydiary=22216733 target=_blank>引用此文</a>)<br>文章来源:<a href='http://www.blogcn.com/user28/reene/blog/22216733.html'>http://www.blogcn.com/user28/reene/blog/22216733.html</a><img src ="http://www.cnitblog.com/reene/aggbug/3173.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-10-09 13:07 <a href="http://www.cnitblog.com/reene/archive/2005/10/09/3173.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>应用HttpClient来对付各种顽固的WEB服务器(2) [转]</title><link>http://www.cnitblog.com/reene/archive/2005/10/09/3174.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sun, 09 Oct 2005 05:07:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/10/09/3174.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/3174.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/10/09/3174.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/3174.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/3174.html</trackback:ping><description><![CDATA[<TABLE style="BORDER-RIGHT: #6595d6 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #6595d6 1px dotted; FONT-SIZE: 12px; BORDER-LEFT: #6595d6 1px dotted; BORDER-BOTTOM: #6595d6 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
<TBODY>
<TR>
<TD style="WORD-WRAP: break-word" bgColor=#e8f4ff>
<P><FONT face="Courier New" size=2>2． 以GET或者POST方式向网页提交参数</FONT></P>
<P><FONT face="Courier New" size=2>其实前面一个最简单的示例中我们已经介绍了如何使用GET或者POST方式来请求一个页面，本小节与之不同的是多了提交时设定页面所需的参数，我们知道如果是GET的请求方式，那么所有参数都直接放到页面的URL后面用问号与页面地址隔开，每个参数用&amp;隔开，例如：<A href="http://java.sun.com?name=liudong&amp;mobile=123456">http://java.sun.com?name=liudong&amp;mobile=123456</A>，但是当使用POST方法时就会稍微有一点点麻烦。本小节的例子演示向如何查询手机号码所在的城市，代码如下：</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;</FONT></P>
<P><FONT face="Courier New" size=2>/*</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;* Created on 2003-12-7 by Liudong</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;*/</FONT></P>
<P><FONT face="Courier New" size=2>package http.demo;</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;</FONT></P>
<P><FONT face="Courier New" size=2>import java.io.IOException;</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;</FONT></P>
<P><FONT face="Courier New" size=2>import org.apache.commons.httpclient.*;</FONT></P>
<P><FONT face="Courier New" size=2>import org.apache.commons.httpclient.methods.*;</FONT></P>
<P><FONT face="Courier New" size=2>/**</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;* 提交参数演示</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;* 该程序连接到一个用于查询手机号码所属地的页面</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;* 以便查询号码段1330227所在的省份以及城市</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;* @author Liudong</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;*/</FONT></P>
<P><FONT face="Courier New" size=2>public class SimpleHttpClient {</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp; public static void main(String[] args) throws IOException</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp; {</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HttpClient client = new HttpClient();</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client.getHostConfiguration().setHost("<A href="http://www.imobile.com.cn">www.imobile.com.cn</A>", 80, "http");</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HttpMethod method = getPostMethod();//使用POST方式提交数据</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client.executeMethod(method);</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //打印服务器返回的状态</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(method.getStatusLine());</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //打印结果页面</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String response =</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new String(method.getResponseBodyAsString().getBytes("8859_1"));</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //打印返回的信息</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(response);</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; method.releaseConnection();</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp; }</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp; /**</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp; * 使用GET方式提交数据</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp; * @return</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp; */</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp; private static HttpMethod getGetMethod(){</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return new GetMethod("/simcard.php?simcard=1330227");</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp; }</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp; /**</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp; * 使用POST方式提交数据</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp; * @return</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp; */</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp; private static HttpMethod getPostMethod(){</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PostMethod post = new PostMethod("/simcard.php");</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NameValuePair simcard = new NameValuePair("simcard","1330227");</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; post.setRequestBody(new NameValuePair[] { simcard});</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return post;</FONT></P>
<P><FONT face="Courier New" size=2>&nbsp;&nbsp;&nbsp; }</FONT></P>
<P><FONT face="Courier New" size=2>}</FONT></P>
<P><FONT face="Courier New" size=2>在上面的例子中页面<A href="http://www.imobile.com.cn/simcard.php">http://www.imobile.com.cn/simcard.php</A>需要一个参数是simcard，这个参数值为手机号码段，即手机号码的前七位，服务器会返回提交的手机号码对应的省份、城市以及其他详细信息。GET的提交方法只需要在URL后加入参数信息，而POST则需要通过NameValuePair类来设置参数名称和它所对应的值</FONT></P>
<P><FONT face="Courier New" size=2>3． 处理页面重定向</FONT></P>
<P><FONT face="Courier New" size=2>在JSP/Servlet编程中response.sendRedirect方法就是使用HTTP协议中的重定向机制。它与JSP中的&lt;jsp:forward …&gt;的区别在于后者是在服务器中实现页面的跳转，也就是说应用容器加载了所要跳转的页面的内容并返回给客户端；而前者是返回一个状态码，这些状态码的可能值见下表，然后客户端读取需要跳转到的页面的URL并重新加载新的页面。就是这样一个过程，所以我们编程的时候就要通过HttpMethod.getStatusCode()方法判断返回值是否为下表中的某个值来判断是否需要跳转。如果已经确认需要进行页面跳转了，那么可以通过读取HTTP头中的location属性来获取新的地址。</FONT></P>
<P><FONT face="Courier New" size=2>状态码<BR>&nbsp;对应HttpServletResponse的常量<BR>&nbsp;详细描述<BR>&nbsp;<BR>301<BR>&nbsp;SC_MOVED_PERMANENTLY<BR>&nbsp;页面已经永久移到另外一个新地址<BR>&nbsp;<BR>302<BR>&nbsp;SC_MOVED_TEMPORARILY<BR>&nbsp;页面暂时移动到另外一个新的地址<BR>&nbsp;<BR>303<BR>&nbsp;SC_SEE_OTHER<BR>&nbsp;客户端请求的地址必须通过另外的URL来访问<BR>&nbsp;<BR>307<BR>&nbsp;SC_TEMPORARY_REDIRECT<BR>&nbsp;同SC_MOVED_TEMPORARILY<BR>&nbsp;</FONT></P><FONT face="Courier New" size=2>
<P><BR>下面的代码片段演示如何处理页面的重定向</P>
<P>client.executeMethod(post);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(post.getStatusLine().toString()); </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; post.releaseConnection();</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //检查是否重定向</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int statuscode = post.getStatusCode();</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (statuscode == HttpStatus.SC_SEE_OTHER) ||</P>
<P>(statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {</P>
<P>//读取新的URL地址</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Header header = post.getResponseHeader("location");</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (header != null) {</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String newuri = header.getValue();</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ((newuri == null) || (newuri.equals("")))</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newuri = "/"; </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GetMethod redirect = new GetMethod(newuri);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; client.executeMethod(redirect);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Redirect:"+ redirect.getStatusLine().toString()); </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; redirect.releaseConnection();</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else </P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("Invalid redirect");</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</P>
<P>我们可以自行编写两个JSP页面，其中一个页面用response.sendRedirect方法重定向到另外一个页面用来测试上面的例子。<BR></FONT></P>
<P><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体"></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体"></P></SPAN></TD></TR></TBODY></TABLE>	(<a href=http://www.blogcn.com/user28/reene/blog/22216676.html target=_blank>浏览全文</a>&nbsp; <a href=http://www.blogcn.com/blog/trackback.asp?mydiary=22216676 target=_blank>引用此文</a>)<br>文章来源:<a href='http://www.blogcn.com/user28/reene/blog/22216676.html'>http://www.blogcn.com/user28/reene/blog/22216676.html</a><img src ="http://www.cnitblog.com/reene/aggbug/3174.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-10-09 13:07 <a href="http://www.cnitblog.com/reene/archive/2005/10/09/3174.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>应用HttpClient来对付各种顽固的WEB服务器(1)   [转]</title><link>http://www.cnitblog.com/reene/archive/2005/10/09/3175.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sun, 09 Oct 2005 05:07:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/10/09/3175.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/3175.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/10/09/3175.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/3175.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/3175.html</trackback:ping><description><![CDATA[<TABLE style="BORDER-RIGHT: #6595d6 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #6595d6 1px dotted; FONT-SIZE: 12px; BORDER-LEFT: #6595d6 1px dotted; BORDER-BOTTOM: #6595d6 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
<TBODY>
<TR>
<TD style="WORD-WRAP: break-word" bgColor=#e8f4ff>
<P>一般的情况下我们都是使用IE或者Navigator浏览器来访问一个WEB服务器，用来浏览页面查看信息或者提交一些数据等等。所访问的这些页面有的仅仅是一些普通的页面，有的需要用户登录后方可使用，或者需要认证以及是一些通过加密方式传输，例如HTTPS。目前我们使用的浏览器处理这些情况都不会构成问题。不过你可能在某些时候需要通过程序来访问这样的一些页面，比如从别人的网页中“偷”一些数据；利用某些站点提供的页面来完成某种功能，例如说我们想知道某个手机号码的归属地而我们自己又没有这样的数据，因此只好借助其他公司已有的网站来完成这个功能，这个时候我们需要向网页提交手机号码并从返回的页面中解析出我们想要的数据来。如果对方仅仅是一个很简单的页面，那我们的程序会很简单，本文也就没有必要大张旗鼓的在这里浪费口舌。但是考虑到一些服务授权的问题，很多公司提供的页面往往并不是可以通过一个简单的URL就可以访问的，而必须经过注册然后登录后方可使用提供服务的页面，这个时候就涉及到COOKIE问题的处理。我们知道目前流行的动态网页技术例如ASP、JSP无不是通过COOKIE来处理会话信息的。为了使我们的程序能使用别人所提供的服务页面，就要求程序首先登录后再访问服务页面，这过程就需要自行处理cookie，想想当你用java.net.HttpURLConnection来完成这些功能时是多么恐怖的事情啊！况且这仅仅是我们所说的顽固的WEB服务器中的一个很常见的“顽固”！再有如通过HTTP来上传文件呢？不需要头疼，这些问题有了“它”就很容易解决了！ </P>
<P></P>
<P>我们不可能列举所有可能的顽固，我们会针对几种最常见的问题进行处理。当然了，正如前面说到的，如果我们自己使用java.net.HttpURLConnection来搞定这些问题是很恐怖的事情，因此在开始之前我们先要介绍一下一个开放源码的项目，这个项目就是Apache开源组织中的httpclient，它隶属于Jakarta的commons项目，目前的版本是2.0RC2。commons下本来已经有一个net的子项目，但是又把httpclient单独提出来，可见http服务器的访问绝非易事。</P>
<P>Commons-httpclient项目就是专门设计来简化HTTP客户端与服务器进行各种通讯编程。通过它可以让原来很头疼的事情现在轻松的解决，例如你不再管是HTTP或者HTTPS的通讯方式，告诉它你想使用HTTPS方式，剩下的事情交给httpclient替你完成。本文会针对我们在编写HTTP客户端程序时经常碰到的几个问题进行分别介绍如何使用httpclient来解决它们，为了让读者更快的熟悉这个项目我们最开始先给出一个简单的例子来读取一个网页的内容，然后循序渐进解决掉前进中的所有问题。</P>
<P>1．&nbsp;读取网页(HTTP/HTTPS)内容</P>
<P>下面是我们给出的一个简单的例子用来访问某个页面</P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">/*</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>* Created on 2003-12-14 by Liudong</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>*/</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">package</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <SPAN style="COLOR: black">http.demo;</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt">&nbsp;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">import</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <SPAN style="COLOR: black">java.io.IOException;</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt">&nbsp;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">import</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <SPAN style="COLOR: black">org.apache.commons.httpclient.*;</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">import</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <SPAN style="COLOR: black">org.apache.commons.httpclient.methods.*;</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f5fbf; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">/**</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN><SPAN style="COLOR: #3f5fbf">*</SPAN> </SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f5fbf; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">最简单的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f5fbf; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">HTTP</SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f5fbf; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">客户端</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f5fbf; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">,</SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f5fbf; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">用来演示通过</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f5fbf; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">GET</SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f5fbf; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">或者</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f5fbf; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">POST</SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f5fbf; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">方式访问某个页面</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN><SPAN style="COLOR: #3f5fbf">*</SPAN> <B><SPAN style="COLOR: #7f9fbf">@author</SPAN></B> <SPAN style="COLOR: #3f5fbf">Liudong</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN><SPAN style="COLOR: #3f5fbf">*/</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">public</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <B><SPAN style="COLOR: #7f0055">class</SPAN></B> <SPAN style="COLOR: black">SimpleClient</SPAN> <SPAN style="COLOR: black">{</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt">&nbsp;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><B><SPAN style="COLOR: #7f0055">public</SPAN></B> <B><SPAN style="COLOR: #7f0055">static</SPAN></B> <B><SPAN style="COLOR: #7f0055">void</SPAN></B> <SPAN style="COLOR: black">main(String[]</SPAN> <SPAN style="COLOR: black">args)</SPAN> <B><SPAN style="COLOR: #7f0055">throws</SPAN></B> <SPAN style="COLOR: black">IOException</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">{</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">HttpClient</SPAN> <SPAN style="COLOR: black">client</SPAN> <SPAN style="COLOR: black">=</SPAN> <B><SPAN style="COLOR: #7f0055">new</SPAN></B> <SPAN style="COLOR: black">HttpClient();</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #3f7f5f">//</SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">设置代理服务器地址和端口</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;</SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1"></SPAN><SPAN style="COLOR: #3f7f5f">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //client.getHostConfiguration().setProxy("proxy_host_addr",proxy_port);</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>//</SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">使用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">GET</SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">方法</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">，如果服务器需要通过</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: #3f7f5f; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt">HTTPS</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">连接，那只需要将下面</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: #3f7f5f; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt">URL</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">中的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: #3f7f5f; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt">http</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">换成</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: #3f7f5f; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt">https</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">HttpMethod</SPAN> <SPAN style="COLOR: black">method</SPAN> <SPAN style="COLOR: black">=</SPAN> <B><SPAN style="COLOR: #7f0055">new</SPAN></B> <SPAN style="COLOR: black">GetMethod(</SPAN><SPAN style="COLOR: #2a00ff">"http://java.sun.com"</SPAN><SPAN style="COLOR: black">);</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"> <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #3f7f5f; mso-highlight: white">//</SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">使用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">POST</SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">方法</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #3f7f5f">//HttpMethod method = new PostMethod("http://java.sun.com");</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"> <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">client.executeMethod(method);</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #3f7f5f">//</SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">打印服务器返回的状态</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">System.out.println(method.getStatusLine());</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #3f7f5f">//</SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">打印返回的信息</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">System.out.println(method.getResponseBodyAsString());</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #3f7f5f">//</SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-highlight: white; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'">释放连接</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">method.releaseConnection();</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">}<BR></SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体">}</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体"></SPAN>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体">在这个例子中首先创建一个HTTP客户端(HttpClient)的实例，然后选择提交的方法是GET或者POST，最后在HttpClient实例上执行提交的方法，最后从所选择的提交方法中读取服务器反馈回来的结果。这就是使用HttpClient的基本流程。其实用一行代码也就可以搞定整个请求的过程，非常的简单！</SPAN></P></TD></TR></TBODY></TABLE>	(<a href=http://www.blogcn.com/user28/reene/blog/22216549.html target=_blank>浏览全文</a>&nbsp; <a href=http://www.blogcn.com/blog/trackback.asp?mydiary=22216549 target=_blank>引用此文</a>)<br>文章来源:<a href='http://www.blogcn.com/user28/reene/blog/22216549.html'>http://www.blogcn.com/user28/reene/blog/22216549.html</a><img src ="http://www.cnitblog.com/reene/aggbug/3175.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-10-09 13:07 <a href="http://www.cnitblog.com/reene/archive/2005/10/09/3175.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>有用的国外开源项目网址 [转]</title><link>http://www.cnitblog.com/reene/archive/2005/10/09/3176.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sun, 09 Oct 2005 05:07:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/10/09/3176.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/3176.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/10/09/3176.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/3176.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/3176.html</trackback:ping><description><![CDATA[<TABLE style="BORDER-RIGHT: #6595d6 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #6595d6 1px dotted; FONT-SIZE: 12px; BORDER-LEFT: #6595d6 1px dotted; BORDER-BOTTOM: #6595d6 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
<TBODY>
<TR>
<TD style="WORD-WRAP: break-word" bgColor=#e8f4ff>
<P>sourceforge <A href="http://www.sourceforge.net">http://www.sourceforge.net</A> <BR>java.net <A href="http://www.java.net">http://www.java.net</A></P>
<P><A href="http://www.eclipse.org">www.eclipse.org</A></P>
<P><A href="http://www.opensource.org">www.opensource.org</A></P>
<P>Lomboz <A href="http://www.objectlearn.com/index.jsp">http://www.objectlearn.com/index.jsp</A> (J2EE plugin for Eclipse)<BR>htmlArea <A href="http://sourceforge.net/projects/itools-htmlarea/">http://sourceforge.net/projects/itools-htmlarea/</A> (所见即所得的在线HTML编辑器)<BR>XmlBuddy <A href="http://www.xmlbuddy.com/">http://www.xmlbuddy.com/</A> (XML Editor plugin for Eclipse)<BR>JFreeChart <A href="http://www.jfree.org/">http://www.jfree.org/</A> (用于生成图表的项目)<BR>EclipseME <A href="http://eclipseme.sourceforge.net/">http://eclipseme.sourceforge.net/</A> (J2ME Developmnt Plugin for Eclipse)<BR>mvnForum <A href="http://sourceforge.net/projects/mvnforum/">http://sourceforge.net/projects/mvnforum/</A> (论坛)<BR>jChatBox <A href="http://www.javazoom.net/index.shtml">http://www.javazoom.net/index.shtml</A> (用servlet实现的WEB聊天引擎)<BR>POI <A href="http://jakarta.apache.org/poi/index.html">http://jakarta.apache.org/poi/index.html</A> (用于处理Excel,WORD等文档的项目)<BR>FileUpload <A href="http://jakarta.apache.org/commons/fileupload/">http://jakarta.apache.org/commons/fileupload/</A> (用于处理HTTP文件上传得项目)<BR>PDFBox <A href="http://sourceforge.net/projects/pdfbox/">http://sourceforge.net/projects/pdfbox/</A> (处理PDF文档的项目)<BR>Lucene <A href="http://jakarta.apache.org/lucene/index.html">http://jakarta.apache.org/lucene/index.html</A> (搜索引擎)<BR>Digester <A href="http://jakarta.apache.org/commons/digester/">http://jakarta.apache.org/commons/digester/</A> (处理XML信息的项目)<BR>DBCP <A href="http://jakarta.apache.org/commons/dbcp/">http://jakarta.apache.org/commons/dbcp/</A> (数据库连接池)<BR>AXIS <A href="http://ws.apache.org/axis/">http://ws.apache.org/axis/</A> (WebService 的实现框架)<BR>Jetspeed <A href="http://portals.apache.org/jetspeed-1/">http://portals.apache.org/jetspeed-1/</A> (Portal)<BR>HSQLDB <A href="http://sourceforge.net/projects/hsqldb/">http://sourceforge.net/projects/hsqldb/</A> (Im memory Database Engine)<BR>CEWOLF <A href="http://sourceforge.net/projects/cewolf/">http://sourceforge.net/projects/cewolf/</A> (一套标签库实现Web报表,使用的是jFreeChart引擎)<BR>Struts Menu <A href="http://sourceforge.net/projects/struts-menu/">http://sourceforge.net/projects/struts-menu/</A> (基于Struts的Web菜单项目)<BR>htmlparser <A href="http://sourceforge.net/projects/htmlparser/">http://sourceforge.net/projects/htmlparser/</A> (用于解析HTML信息的项目)<BR>Mondrian <A href="http://sourceforge.net/projects/mondrian/(Open">http://sourceforge.net/projects/mondrian/(Open</A> Source OLAP Database)<BR>ProGuard <A href="http://sourceforge.net/projects/proguard/(Java">http://sourceforge.net/projects/proguard/(Java</A>的混淆器)<BR>InfoGlue <A href="http://sourceforge.net/projects/infoglue/">http://sourceforge.net/projects/infoglue/</A> (J2EE 内容管理系统)<BR>JPivot <A href="http://sourceforge.net/projects/jpivot/">http://sourceforge.net/projects/jpivot/</A> (基于WEB的OLAP 展现)<BR><A href="http://java-source.net/">http://java-source.net/</A></P>
<P>(5)几个常用JAVA开源项目的地址荟萃:<BR>Lomboz&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <A href="http://www.objectlearn.com/index.jsp">http://www.objectlearn.com/index.jsp</A> (J2EE plugin for Eclipse)<BR>htmlArea&nbsp;&nbsp;&nbsp; <A href="http://sourceforge.net/projects/itools-htmlarea/">http://sourceforge.net/projects/itools-htmlarea/</A> (所见即所得的在线HTML编辑器)<BR>XmlBuddy&nbsp;&nbsp; <A href="http://www.xmlbuddy.com/">http://www.xmlbuddy.com/</A> (XML Editor plugin for Eclipse)<BR>JFreeChart <A href="http://www.jfree.org/">http://www.jfree.org/</A> (用于生成图表的项目)<BR>EclipseME&nbsp;&nbsp; <A href="http://eclipseme.sourceforge.net/">http://eclipseme.sourceforge.net/</A> (J2ME Developmnt Plugin for Eclipse)<BR>mvnForum&nbsp; <A href="http://sourceforge.net/projects/mvnforum/">http://sourceforge.net/projects/mvnforum/</A> (论坛)<BR>jChatBox&nbsp;&nbsp;&nbsp; <A href="http://www.javazoom.net/index.shtml">http://www.javazoom.net/index.shtml</A> (用servlet实现的WEB聊天引擎)<BR>POI&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <A href="http://jakarta.apache.org/poi/index.html">http://jakarta.apache.org/poi/index.html</A> (用于处理Excel,WORD等文档的项目)<BR>FileUpload <A href="http://jakarta.apache.org/commons/fileupload/">http://jakarta.apache.org/commons/fileupload/</A> (用于处理HTTP文件上传得项目)<BR>PDFBox&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <A href="http://sourceforge.net/projects/pdfbox/">http://sourceforge.net/projects/pdfbox/</A> (处理PDF文档的项目)<BR>Lucene&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <A href="http://jakarta.apache.org/lucene/index.html">http://jakarta.apache.org/lucene/index.html</A> (搜索引擎)<BR>Digester&nbsp;&nbsp;&nbsp; <A href="http://jakarta.apache.org/commons/digester/">http://jakarta.apache.org/commons/digester/</A> (处理XML信息的项目)<BR>DBCP&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <A href="http://jakarta.apache.org/commons/dbcp/">http://jakarta.apache.org/commons/dbcp/</A>&nbsp; (数据库连接池)<BR>AXIS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <A href="http://ws.apache.org/axis/">http://ws.apache.org/axis/</A> (WebService 的实现框架)<BR>Jetspeed&nbsp;&nbsp; <A href="http://portals.apache.org/jetspeed-1/">http://portals.apache.org/jetspeed-1/</A> (Portal)<BR>HSQLDB&nbsp;&nbsp;&nbsp; <A href="http://sourceforge.net/projects/hsqldb/">http://sourceforge.net/projects/hsqldb/</A> (Im memory Database Engine)<BR>CEWOLF&nbsp;&nbsp; <A href="http://sourceforge.net/projects/cewolf/">http://sourceforge.net/projects/cewolf/</A>&nbsp; (一套标签库实现Web报表,使用的是jFreeChart引擎)<BR>Struts Menu <A href="http://sourceforge.net/projects/struts-menu/">http://sourceforge.net/projects/struts-menu/</A>&nbsp; (基于Struts的Web菜单项目)<BR>htmlparser&nbsp;&nbsp; <A href="http://sourceforge.net/projects/htmlparser/">http://sourceforge.net/projects/htmlparser/</A>&nbsp;&nbsp; (用于解析HTML信息的项目)<BR>Mondrian&nbsp;&nbsp;&nbsp;&nbsp; <A href="http://sourceforge.net/projects/mondrian/">http://sourceforge.net/projects/mondrian/</A>&nbsp; (Open Source OLAP Database)<BR>ProGuard&nbsp;&nbsp;&nbsp;&nbsp; <A href="http://sourceforge.net/projects/proguard/">http://sourceforge.net/projects/proguard/</A> (Java的混淆器)<BR>InfoGlue&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <A href="http://sourceforge.net/projects/infoglue/">http://sourceforge.net/projects/infoglue/</A>&nbsp; (J2EE 内容管理系统)<BR>JPivot&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <A href="http://sourceforge.net/projects/jpivot/">http://sourceforge.net/projects/jpivot/</A>&nbsp; (基于WEB的OLAP 展现)<BR>HttpClient&nbsp;&nbsp; <A href="http://jakarta.apache.org/commons/httpclient/">http://jakarta.apache.org/commons/httpclient/</A>&nbsp; 处理http客户端的接口<BR></P></TD></TR></TBODY></TABLE>	(<a href=http://www.blogcn.com/user28/reene/blog/22215741.html target=_blank>浏览全文</a>&nbsp; <a href=http://www.blogcn.com/blog/trackback.asp?mydiary=22215741 target=_blank>引用此文</a>)<br>文章来源:<a href='http://www.blogcn.com/user28/reene/blog/22215741.html'>http://www.blogcn.com/user28/reene/blog/22215741.html</a><img src ="http://www.cnitblog.com/reene/aggbug/3176.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-10-09 13:07 <a href="http://www.cnitblog.com/reene/archive/2005/10/09/3176.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>应用HttpClient来对付各种顽固的WEB服务器(4) [转]</title><link>http://www.cnitblog.com/reene/archive/2005/10/09/3172.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sun, 09 Oct 2005 05:07:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/10/09/3172.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/3172.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/10/09/3172.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/3172.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/3172.html</trackback:ping><description><![CDATA[<TABLE style="BORDER-RIGHT: #6595d6 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #6595d6 1px dotted; FONT-SIZE: 12px; BORDER-LEFT: #6595d6 1px dotted; BORDER-BOTTOM: #6595d6 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
<TBODY>
<TR>
<TD style="WORD-WRAP: break-word" bgColor=#e8f4ff>
<P><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体">6．&nbsp;通过HTTP上传文件</SPAN></P>
<P><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体">httpclient使用了单独的一个HttpMethod子类来处理文件的上传，这个类就是MultipartPostMethod，该类已经封装了文件上传的细节，我们要做的仅仅是告诉它我们要上传文件的全路径即可，下面的代码片段演示如何使用这个类。</SPAN></P><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体">
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">MultipartPostMethod</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <SPAN style="COLOR: black">filePost</SPAN> <SPAN style="COLOR: black">=</SPAN> <B><SPAN style="COLOR: #7f0055">new</SPAN></B> <SPAN style="COLOR: black">MultipartPostMethod(targetURL);</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">filePost.addParameter(</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #2a00ff; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">"fileName"</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">,</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <SPAN style="COLOR: black">targetFilePath);</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">HttpClient</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <SPAN style="COLOR: black">client</SPAN> <SPAN style="COLOR: black">=</SPAN> <B><SPAN style="COLOR: #7f0055">new</SPAN></B> <SPAN style="COLOR: black">HttpClient();</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">//</SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-highlight: white">由于要上传的文件可能比较大</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">,</SPAN><SPAN style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #3f7f5f; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-highlight: white">因此在此设置最大的连接超时时间</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">client.getHttpConnectionManager().</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="COLOR: black">getParams().setConnectionTimeout(5000);</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P><B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体">int</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体"> <SPAN style="COLOR: black">status</SPAN> <SPAN style="COLOR: black">=</SPAN> <SPAN style="COLOR: black">client.executeMethod(filePost);</SPAN></SPAN><BR></SPAN></P>
<P>上面代码中，targetFilePath即为要上传的文件所在的路径。</P>
<P>7．&nbsp;访问启用认证的页面</P>
<P>我们经常会碰到这样的页面，当访问它的时候会弹出一个浏览器的对话框要求输入用户名和密码后方可，这种用户认证的方式不同于我们在前面介绍的基于表单的用户身份验证。这是HTTP的认证策略，httpclient支持三种认证方式包括：基本、摘要以及NTLM认证。其中基本认证最简单、通用但也最不安全；摘要认证是在HTTP 1.1中加入的认证方式，而NTLM则是微软公司定义的而不是通用的规范，最新版本的NTLM是比摘要认证还要安全的一种方式。</P>
<P>下面例子是从httpclient的CVS服务器中下载的，它简单演示如何访问一个认证保护的页面：<BR></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">import</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <SPAN style="COLOR: black">org.apache.commons.httpclient.HttpClient;</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">import</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <SPAN style="COLOR: black">org.apache.commons.httpclient.UsernamePasswordCredentials;</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">import</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <SPAN style="COLOR: black">org.apache.commons.httpclient.methods.GetMethod;</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt">&nbsp;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #7f0055; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">public</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <B><SPAN style="COLOR: #7f0055">class</SPAN></B> <SPAN style="COLOR: black">BasicAuthenticationExample</SPAN> <SPAN style="COLOR: black">{</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><B><SPAN style="COLOR: #7f0055">public</SPAN></B> <SPAN style="COLOR: black">BasicAuthenticationExample()</SPAN> <SPAN style="COLOR: black">{</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">}</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN><SPAN style="mso-spacerun: yes">&nbsp;</SPAN><B><SPAN style="COLOR: #7f0055">public</SPAN></B> <B><SPAN style="COLOR: #7f0055">static</SPAN></B> <B><SPAN style="COLOR: #7f0055">void</SPAN></B> <SPAN style="COLOR: black">main(String[]</SPAN> <SPAN style="COLOR: black">args)</SPAN> <B><SPAN style="COLOR: #7f0055">throws</SPAN></B> <SPAN style="COLOR: black">Exception</SPAN> <SPAN style="COLOR: black">{</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">HttpClient</SPAN> <SPAN style="COLOR: black">client</SPAN> <SPAN style="COLOR: black">=</SPAN> <B><SPAN style="COLOR: #7f0055">new</SPAN></B> <SPAN style="COLOR: black">HttpClient();</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">client.getState().setCredentials(</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #2a00ff">"www.verisign.com"</SPAN><SPAN style="COLOR: black">,</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: #2a00ff">"realm"</SPAN><SPAN style="COLOR: black">,</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><B><SPAN style="COLOR: #7f0055">new</SPAN></B> <SPAN style="COLOR: black">UsernamePasswordCredentials(</SPAN><SPAN style="COLOR: #2a00ff">"username"</SPAN><SPAN style="COLOR: black">,</SPAN> <SPAN style="COLOR: #2a00ff">"password"</SPAN><SPAN style="COLOR: black">)</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">);</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">GetMethod</SPAN> <SPAN style="COLOR: black">get</SPAN> <SPAN style="COLOR: black">=</SPAN> <B><SPAN style="COLOR: #7f0055">new</SPAN></B> <SPAN style="COLOR: black">GetMethod(</SPAN><SPAN style="COLOR: #2a00ff">"https://www.verisign.com/products/index.html"</SPAN><SPAN style="COLOR: black">);</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">get.setDoAuthentication(</SPAN> <B><SPAN style="COLOR: #7f0055">true</SPAN></B> <SPAN style="COLOR: black">);</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><B><SPAN style="COLOR: #7f0055">int</SPAN></B> <SPAN style="COLOR: black">status</SPAN> <SPAN style="COLOR: black">=</SPAN> <SPAN style="COLOR: black">client.executeMethod(</SPAN> <SPAN style="COLOR: black">get</SPAN> <SPAN style="COLOR: black">);</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">System.out.println(status+</SPAN><SPAN style="COLOR: #2a00ff">"\n"</SPAN><SPAN style="COLOR: black">+</SPAN> <SPAN style="COLOR: black">get.getResponseBodyAsString());</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">get.releaseConnection();</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: black">}</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体">}</SPAN></P>
<P><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体">8．&nbsp;多线程模式下使用httpclient</SPAN></P>
<P><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体">多线程同时访问httpclient，例如同时从一个站点上下载多个文件。对于同一个HttpConnection同一个时间只能有一个线程访问，为了保证多线程工作环境下不产生冲突，httpclient使用了一个多线程连接管理器的类：MultiThreadedHttpConnectionManager，要使用这个类很简单，只需要在构造HttpClient实例的时候传入即可，代码如下：</SPAN></P><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体">
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white">MultiThreadedHttpConnectionManager</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"> <SPAN style="COLOR: black">connectionManager</SPAN> <SPAN style="COLOR: black">=</SPAN> </SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align=left><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt; mso-highlight: white"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp; </SPAN><B><SPAN style="COLOR: #7f0055">new</SPAN></B> <SPAN style="COLOR: black">MultiThreadedHttpConnectionManager();</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"><o:p></o:p></SPAN></P>
<P><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体"><SPAN style="COLOR: black">HttpClient</SPAN> <SPAN style="COLOR: black">client</SPAN> <SPAN style="COLOR: black">=</SPAN> <B><SPAN style="COLOR: #7f0055">new</SPAN></B> <SPAN style="COLOR: black">HttpClient(connectionManager);</SPAN></SPAN></P>
<P><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体"><SPAN style="COLOR: black">以后尽管访问client实例即可。</SPAN></SPAN></P>
<P><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体"><SPAN style="COLOR: black">参考资料：</SPAN></SPAN></P>
<P><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; FONT-FAMILY: 'Courier New'; mso-highlight: white; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA; mso-fareast-font-family: 宋体"><SPAN style="COLOR: black">httpclient首页：&nbsp;&nbsp;&nbsp;&nbsp;<A href="http://jakarta.apache.org/commons/httpclient/">http://jakarta.apache.org/commons/httpclient/</A><BR>关于NTLM是如何工作：&nbsp;&nbsp;<A href="http://davenport.sourceforge.net/ntlm.html">http://davenport.sourceforge.net/ntlm.html</A> </SPAN></SPAN><BR></SPAN><BR></P></TD></TR></TBODY></TABLE>	(<a href=http://www.blogcn.com/user28/reene/blog/22216776.html target=_blank>浏览全文</a>&nbsp; <a href=http://www.blogcn.com/blog/trackback.asp?mydiary=22216776 target=_blank>引用此文</a>)<br>文章来源:<a href='http://www.blogcn.com/user28/reene/blog/22216776.html'>http://www.blogcn.com/user28/reene/blog/22216776.html</a><img src ="http://www.cnitblog.com/reene/aggbug/3172.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-10-09 13:07 <a href="http://www.cnitblog.com/reene/archive/2005/10/09/3172.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>jfree中字体的调整</title><link>http://www.cnitblog.com/reene/archive/2005/10/09/3178.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sun, 09 Oct 2005 05:07:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/10/09/3178.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/3178.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/10/09/3178.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/3178.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/3178.html</trackback:ping><description><![CDATA[<TABLE style="BORDER-RIGHT: #6595d6 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #6595d6 1px dotted; FONT-SIZE: 12px; BORDER-LEFT: #6595d6 1px dotted; BORDER-BOTTOM: #6595d6 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
<TBODY>
<TR>
<TD style="WORD-WRAP: break-word" bgColor=#e8f4ff>
<P>JFreeChart chart = ChartFactory.createVerticalBarChart3D(title, domain, range, dataset,true,true,false);</P>
<P>chart.setBackgroundPaint(new GradientPaint(0.0F, 0.0F, Color.white, 1000F, 0.0F, Color.red));<BR>chart.setTitle(new TextTitle(title, new Font("隶书", Font.ITALIC, 15)));</P>
<P>Font font=new Font("黑体",Font.TRUETYPE_FONT, 12);</P>
<P>StandardLegend legend = (StandardLegend) chart.getLegend();<BR>legend.setItemFont(font);</P>
<P>CategoryPlot plot = (CategoryPlot)chart.getPlot();<BR>plot.setForegroundAlpha(0.9F);</P>
<P>CategoryAxis domain_axis = plot.getDomainAxis();<BR>domain_axis.setTickLabelFont(font);</P>
<P>ValueAxis value_axis=plot.getRangeAxis();<BR>value_axis.setTickLabelFont(font);</P>
<P>&nbsp;</P></TD></TR></TBODY></TABLE>	(<a href=http://www.blogcn.com/user28/reene/blog/22155047.html target=_blank>浏览全文</a>&nbsp; <a href=http://www.blogcn.com/blog/trackback.asp?mydiary=22155047 target=_blank>引用此文</a>)<br>文章来源:<a href='http://www.blogcn.com/user28/reene/blog/22155047.html'>http://www.blogcn.com/user28/reene/blog/22155047.html</a><img src ="http://www.cnitblog.com/reene/aggbug/3178.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-10-09 13:07 <a href="http://www.cnitblog.com/reene/archive/2005/10/09/3178.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>使用JFreeChart创建图象</title><link>http://www.cnitblog.com/reene/archive/2005/10/09/3179.html</link><dc:creator>快乐着飞舞着</dc:creator><author>快乐着飞舞着</author><pubDate>Sun, 09 Oct 2005 05:07:00 GMT</pubDate><guid>http://www.cnitblog.com/reene/archive/2005/10/09/3179.html</guid><wfw:comment>http://www.cnitblog.com/reene/comments/3179.html</wfw:comment><comments>http://www.cnitblog.com/reene/archive/2005/10/09/3179.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/reene/comments/commentRss/3179.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/reene/services/trackbacks/3179.html</trackback:ping><description><![CDATA[<TABLE style="BORDER-RIGHT: #6595d6 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #6595d6 1px dotted; FONT-SIZE: 12px; BORDER-LEFT: #6595d6 1px dotted; BORDER-BOTTOM: #6595d6 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
<TBODY>
<TR>
<TD style="WORD-WRAP: break-word" bgColor=#e8f4ff>
<P>使用JFreeChart创建图象&nbsp; <BR>文档性质： 转载&nbsp; 发表时间：2003-5-21 快速返回 <BR>作者:孤魂一笑(<A href="mailto:bingo_ge@hotmail.com">bingo_ge@hotmail.com</A>) 日期：2003-05-08 <BR>一：jfreechart介绍<BR>jfreechart是一个免费创建图片的java工具.可以创建如下图形：<BR>饼图(pie charts;)<BR>曲线图(line charts )<BR>柱状图(horizontal/vertical bar charts)<BR>甘特图(Gantt charts; )<BR>XY plots and scatter plots; <BR>time series, high/low/open/close charts and candle stick charts; <BR>combination charts; <BR>Pareto charts; <BR>bubble charts; <BR>wind plots, meter charts and symbol charts; <BR>从以下地址可以看到jfreechart可以创建的图形类型<BR><A href="http://www.jfree.org/jfreechart/samples.html">http://www.jfree.org/jfreechart/samples.html</A><BR>sourceforge有一个基于jfreechart的项目Cewolf可以很方便的在jsp/servlet中创建图片<BR>jfreechart目前(2003-05-08)版本为0.98<BR>希望得到详细的信息或下载jfreechart请访问如下站点:<BR><A href="http://www.jfree.org/jfreechart/">http://www.jfree.org/jfreechart/</A></P>
<P>二：特别说明:<BR>jfreechart是一个开源项目，但是文档是需要40美金去购买的。<BR>还有一个很重要的问题,jfreechart如果使用中文，他使用的默认字体<BR>显示出来的中文会很模糊，你可能需要修改源代码。<BR>下面我就举几个简单的例子说明一下如何使用jfreechart创建图片<BR>在开发中有可能会导入以下的类<BR>import com.jrefinery.chart.ChartFactory;<BR>import com.jrefinery.chart.ChartUtilities;<BR>import com.jrefinery.chart.JFreeChart;<BR>import com.jrefinery.chart.TextTitle;<BR>import com.jrefinery.chart.axis.NumberAxis;<BR>import com.jrefinery.chart.plot.CategoryPlot;<BR>import com.jrefinery.chart.plot.PiePlot;<BR>import com.jrefinery.data.Day;<BR>import com.jrefinery.data.DefaultCategoryDataset;<BR>import com.jrefinery.data.DefaultPieDataset;<BR>import com.jrefinery.data.TimeSeries;<BR>import com.jrefinery.data.TimeSeriesCollection;<BR>import com.jrefinery.data.TimeSeriesDataPair;<BR>在0.98以后包由com.jrefinery.*改变为:org.jfree</P>
<P>三：创建饼图<BR>//图片标题<BR>String title = "空调2002年市场占有率";<BR>//设定数据源<BR>DefaultPieDataset piedata = new DefaultPieDataset();<BR>//第一个参数为名称，第二个参数是double数<BR>piedata.setValue("联想", 27.3);<BR>piedata.setValue("长城", 12.2);<BR>piedata.setValue("海尔", 5.5);<BR>piedata.setValue("美的", 17.1);<BR>piedata.setValue("松下", 9.0);<BR>piedata.setValue("科龙", 19.0);<BR>//创建JFreeChart，都使用ChartFactory来创建JFreeChart,很标准的工厂设计模式<BR>JFreeChart chart =<BR>ChartFactory.createPieChart(title, piedata, true, true, true);<BR>//设定图片标题<BR>chart.setTitle(new TextTitle(title, new Font("隶书", Font.ITALIC, 15)));<BR>//chart.addSubtitle(new TextTitle("2002财年分析", new Font("隶书", Font.ITALIC, 12)));<BR>//设定背景<BR>chart.setBackgroundPaint(Color.white);<BR>//chart.s<BR>//饼图使用一个PiePlot <BR>PiePlot pie = (PiePlot)chart.getPlot();<BR>//pie.setSectionLabelType(PiePlot.NAME_AND_PERCENT_LABELS);<BR>pie.setSectionLabelType(PiePlot.NAME_AND_VALUE_LABELS);<BR>//设定显示格式(名称加百分比或数值)<BR>pie.setPercentFormatString("#,###0.0#%");<BR>//设定百分比显示格式<BR>pie.setBackgroundPaint(Color.white);<BR>pie.setSectionLabelFont(new Font("黑体", Font.TRUETYPE_FONT, 12));<BR>//设定背景透明度（0-1.0之间）<BR>pie.setBackgroundAlpha(0.6f);<BR>//设定前景透明度（0-1.0之间）<BR>pie.setForegroundAlpha(0.90f);<BR>//输出文件到指定目录<BR>String rfname = MathUtil.getRoundCode(12) + ".jpeg";<BR>String fileName = "d:/test/" + rfname;<BR>try {<BR>//可以保存文件为jpg或png格式。<BR>ChartUtilities.saveChartAsJPEG(new File(fileName), 100, chart, 600, 600);<BR>//第一个参数为文件名<BR>//第二个参数质量<BR>//第三个参数为哪个chart创建图片<BR>//第四个宽度<BR>//第五个高度<BR>} catch (IOException exz) {<BR>System.out.print("....Cant’t Create image File");<BR>}</P>
<P>其实使用JFreeChart创建图片很简单，不同的的图片类型区别在于设置数据集</P>
<P>四：创建曲线图<BR>// create a default chart based on some sample data...<BR>//曲线图标题<BR>String title = "趋势分析";<BR>//曲线图X轴提示<BR>String domain = "月份走势";<BR>//曲线图Y轴提示<BR>String range = "应收余额";<BR>//曲线图自标题<BR>String subtitleStr = "2003财年分析";<BR>//创建时间数据源<BR>//每一个TimeSeries在图上是一条曲线<BR>TimeSeries ca = new TimeSeries("用友");<BR>for (int i = 1999; i &lt; 2005; i++) {<BR>for (int mon = 0; mon &lt; 12; mon++) {<BR>//ca.add(new Month(mon + 1, i), new Double(500 + Math.random() * 100));<BR>//TimeSeriesDataPair是一个时间点的数值体现<BR>ca.add(<BR>new TimeSeriesDataPair(<BR>new Day(1, mon + 1, i),<BR>new Double(500 + Math.random() * 100)));<BR>}<BR>}</P>
<P>TimeSeries ibm = new TimeSeries("金碟");<BR>for (int i = 1999; i &lt; 2005; i++) {<BR>for (int mon = 0; mon &lt; 12; mon++) {<BR>//ibm.add(new Month(mon+1,i),new Double(400-Math.random()*100));<BR>ibm.add(<BR>new TimeSeriesDataPair(<BR>new Day(1, mon + 1, i),<BR>new Double(400 - Math.random() * 100)));<BR>}<BR>}</P>
<P>TimeSeries king = new TimeSeries("东软");<BR>for (int i = 1999; i &lt; 2005; i++) {<BR>for (int mon = 0; mon &lt; 12; mon++) {<BR>//ibm.add(new Month(mon+1,i),new Double(400-Math.random()*100));<BR>king.add(<BR>new TimeSeriesDataPair(<BR>new Day(1, mon + 1, i),<BR>new Double(300 - Math.random() * 100)));<BR>}<BR>}<BR>//时间曲线数据集合<BR>TimeSeriesCollection dataset = new TimeSeriesCollection();<BR>dataset.addSeries(ca);<BR>dataset.addSeries(ibm);<BR>dataset.addSeries(king);<BR>//dataset.addSeries(jpy);<BR>//dataset.addSeries(mav);<BR>//时间曲线元素<BR>JFreeChart chart =<BR>ChartFactory.createTimeSeriesChart(<BR>title,<BR>domain,<BR>range,<BR>dataset,<BR>true,<BR>true,<BR>false);<BR>// then customise it a little...<BR>TextTitle subtitle =<BR>new TextTitle(subtitleStr, new Font("黑体", Font.BOLD, 12));<BR>chart.addSubtitle(subtitle);<BR>chart.setTitle(new TextTitle(title, new Font("隶书", Font.ITALIC, 15)));<BR>//pie.setSeriesLabelFont(new Font("黑体", Font.BOLD, 15));<BR>chart.setBackgroundPaint(<BR>new GradientPaint(0, 0, Color.white, 0, 1000, Color.blue));<BR>//sysout<BR>//输出文件到指定目录<BR>String rfname = MathUtil.getRoundCode(22) + ".jpeg";<BR>String fileName = "d:/test/" + rfname;<BR>try {<BR>//for<BR>//System.out.println();<BR>ChartUtilities.saveChartAsJPEG(new File(fileName), 100, chart, 600, 600);<BR>// log.info("....Create image File:" + fileName);<BR>} catch (IOException exz) {<BR>System.out.print("....Cant’t Create image File");<BR>}</P>
<P><BR>五：创建柱状图<BR>String title = "柱状图测试";<BR>String domain = "单位比较";<BR>String range = "数值";<BR>//CategoryDataset data = DemoDatasetFactory.createCategoryDataset();<BR>DefaultCategoryDataset data = new DefaultCategoryDataset();<BR>for (int r = 0; r &lt; 5; r++) {<BR>String rowKey = "单位 [" + (r + 1)+"]" ;<BR>//第一层循环：分析对象<BR>for (int c = 0; c &lt; 6; c++) {<BR>//第二层循环：分析对象在时间点上的数据<BR>String columnKey = "2001年" + (c + 1) + "月";<BR>data.addValue(new Double(r * c + 5), rowKey, columnKey);<BR>}<BR>}</P>
<P>JFreeChart chart =<BR>ChartFactory.createVerticalBarChart(<BR>title,<BR>domain,<BR>range,<BR>data,<BR>true,<BR>true,<BR>false);<BR>// then customise it a little...<BR>chart.setBackgroundPaint(<BR>new GradientPaint(0, 0, Color.white, 1000, 0, Color.red));<BR>chart.setTitle(new TextTitle(title, new Font("隶书", Font.ITALIC, 15)));<BR>CategoryPlot plot = (CategoryPlot)chart.getPlot();<BR>plot.setForegroundAlpha(0.9f);<BR>plot.setValueLabelFont(new Font("黑体", Font.TRUETYPE_FONT, 12));<BR>//plot.setSectionLabelFont(new Font("黑体", Font.TRUETYPE_FONT, 12));<BR>//注意以下代码<BR>NumberAxis verticalAxis = (NumberAxis)plot.getRangeAxis();<BR>verticalAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());<BR>// 输出文件到指定目录<BR>String rfname = MathUtil.getRoundCode(22) + "b.jpeg";<BR>String fileName = "d:/test/" + rfname;<BR>try {<BR>ChartUtilities.saveChartAsJPEG(new File(fileName), 100, chart, 600, 600);<BR>// log.info("....Create image File:" + fileName);<BR>} catch (IOException exz) {<BR>System.out.print("....Cant’t Create image File");<BR>}</P>
<P>六：结束语<BR>个人感觉JFreeChart可以满足大部分图片创建的需要，美中不足的是：对字体的设置做的<BR>不够好，特别是使用中文的时候字体很不清晰。因为这个原因建议你自己去修改他的源代码<BR>最好使用properties文件去设置字体.还有就是文档要钱所以要多花点时间去看源代码。或多<BR>上社区.因为时间等原因我只介绍了三种图片的创建，其他类型的图片可以参考jfreechart提<BR>供的例子。 <BR></P></TD></TR></TBODY></TABLE>	(<a href=http://www.blogcn.com/user28/reene/blog/22154859.html target=_blank>浏览全文</a>&nbsp; <a href=http://www.blogcn.com/blog/trackback.asp?mydiary=22154859 target=_blank>引用此文</a>)<br>文章来源:<a href='http://www.blogcn.com/user28/reene/blog/22154859.html'>http://www.blogcn.com/user28/reene/blog/22154859.html</a><img src ="http://www.cnitblog.com/reene/aggbug/3179.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/reene/" target="_blank">快乐着飞舞着</a> 2005-10-09 13:07 <a href="http://www.cnitblog.com/reene/archive/2005/10/09/3179.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>在eclipse+myeclipse+weblogic下EJB开发所要注意的问题 [转]</title><link>http://www.cnitblog.com/reene/archive/2005/1