renxiao2003 发表于 2011-12-21 08:44

HttpClient4 Post XML到一个服务器上

<DIV>
<DIV class="CopyrightStatement lh22">原创作品,允许转载,转载时请务必以超链接形式标明文章 <A style="TEXT-DECORATION: underline" href="/62575/347157" target=_blank>原始出处</A> 、作者信息和本声明。否则将追究法律责任。<a href="/62575/347157" target="_blank">http://lavasoft.blog.51cto.com/62575/347157</A> </DIV>
<DIV class=showContent>
<DIV><STRONG><FONT color=#ff0000 size=3>HttpClient4 Post XML到一个服务器上</FONT></STRONG></DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV>现在网上介绍的HttpClient基本上全是3.x版本的内容,HttpClient4的API变化相对3已经变化很大,对HttpClient4做了简单的研究后,完成了一个HttpClient4 Post XML功能。</DIV>
<DIV>&nbsp;</DIV>
<DIV>对于POST方式,最先想到的就是表单提交了,POST XML自然想到的就是定义一个变量名,比如叫xmldata,然后将这个参数的值POST出去,在服务端接收的时候,自然也是通过 requset.getParameter("xmldata")方式来接收。</DIV>
<DIV>&nbsp;</DIV>
<DIV>现在我在这里要做的不是通过上面的方式,而是不指定参数名来Post,实际上就是将一个流写入请求。</DIV>
<DIV>&nbsp;</DIV>
<DIV>下面是具体的实现方式:</DIV>
<DIV>&nbsp;</DIV>
<DIV>1、参数名方式POST XML数据</DIV>
<DIV>
<DIV style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 16px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 4px; FONT-FAMILY: verdana, 宋体; COLOR: #000000; FONT-SIZE: 10pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><FONT color=#0000ff>import</FONT> org.apache.http.*; <BR><FONT color=#0000ff>import</FONT> org.apache.http.client.entity.UrlEncodedFormEntity; <BR><FONT color=#0000ff>import</FONT> org.apache.http.client.methods.HttpPost; <BR><FONT color=#0000ff>import</FONT> org.apache.http.impl.client.DefaultHttpClient; <BR><FONT color=#0000ff>import</FONT> org.apache.http.message.BasicNameValuePair; <BR><FONT color=#0000ff>import</FONT> org.apache.http.client.*; <BR><BR><FONT color=#0000ff>import</FONT> java.io.IOException; <BR><FONT color=#0000ff>import</FONT> java.io.InputStreamReader; <BR><FONT color=#0000ff>import</FONT> java.io.UnsupportedEncodingException; <BR><FONT color=#0000ff>import</FONT> java.util.*; <BR><BR><FONT color=#008000>/** <BR>* 通过指定参数名的方式POST XML <BR>* <BR>* @author leizhimin 2010-7-8 22:29:28 <BR>*/</FONT> <BR><FONT color=#0000ff>public</FONT> <FONT color=#0000ff>class</FONT> TestPost { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>public</FONT> <FONT color=#0000ff>static</FONT> <FONT color=#0000ff>void</FONT> main(String[] args) <FONT color=#0000ff>throws</FONT> IOException { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpClient httpclient = <FONT color=#0000ff>new</FONT> DefaultHttpClient(); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpPost httppost = <FONT color=#0000ff>new</FONT> HttpPost(<FONT color=#800000>"http:<FONT color=#008000>//localhost:8080/waitsrv/GenXmlServlet"); </FONT><BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List&lt;NameValuePair&gt; formparams = <FONT color=#0000ff>new</FONT> ArrayList&lt;NameValuePair&gt;(); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;formparams.add(<FONT color=#0000ff>new</FONT> BasicNameValuePair(<FONT color=#800000>"xmldate"</FONT>, <FONT color=#800000>"&lt;html&gt;你好啊啊&lt;/html&gt;"</FONT>)); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;formparams.add(<FONT color=#0000ff>new</FONT> BasicNameValuePair(<FONT color=#800000>"info"</FONT>, <FONT color=#800000>"xxxxxxxxx"</FONT>)); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UrlEncodedFormEntity entity = <FONT color=#0000ff>new</FONT> UrlEncodedFormEntity(formparams, <FONT color=#800000>"GBK"</FONT>); <BR><FONT color=#008000>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;entity.setContentType("text/xml; charset=GBK"); </FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;httppost.setEntity(entity); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpResponse response = httpclient.execute(httppost); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpEntity resEntity = response.getEntity(); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InputStreamReader reader = <FONT color=#0000ff>new</FONT> InputStreamReader(resEntity.getContent(), <FONT color=#800000>"ISO-8859-1"</FONT>); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>char</FONT>[] buff = <FONT color=#0000ff>new</FONT> <FONT color=#0000ff>char</FONT>; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>int</FONT> length = 0; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>while</FONT> ((length = reader.read(buff)) != -1) { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<FONT color=#0000ff>new</FONT> String(buff, 0, length)); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;httpclient.getConnectionManager().shutdown(); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <BR>}</DIV></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV>2、不指定参数名的方式来POST数据</DIV>
<DIV>
<DIV style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 16px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 4px; FONT-FAMILY: verdana, 宋体; COLOR: #000000; FONT-SIZE: 10pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><FONT color=#0000ff>import</FONT> org.apache.http.HttpEntity; <BR><FONT color=#0000ff>import</FONT> org.apache.http.HttpResponse; <BR><FONT color=#0000ff>import</FONT> org.apache.http.NameValuePair; <BR><FONT color=#0000ff>import</FONT> org.apache.http.client.HttpClient; <BR><FONT color=#0000ff>import</FONT> org.apache.http.client.entity.UrlEncodedFormEntity; <BR><FONT color=#0000ff>import</FONT> org.apache.http.client.methods.HttpPost; <BR><FONT color=#0000ff>import</FONT> org.apache.http.impl.client.DefaultHttpClient; <BR><FONT color=#0000ff>import</FONT> org.apache.http.message.BasicNameValuePair; <BR><FONT color=#0000ff>import</FONT> org.apache.http.entity.*; <BR><BR><BR><FONT color=#0000ff>import</FONT> java.io.IOException; <BR><FONT color=#0000ff>import</FONT> java.io.InputStreamReader; <BR><FONT color=#0000ff>import</FONT> java.util.ArrayList; <BR><FONT color=#0000ff>import</FONT> java.util.List; <BR><BR><FONT color=#008000>/** <BR>* 不指定参数名的方式来POST数据 <BR>* <BR>* @author leizhimin 2010-7-8 3:22:53 <BR>*/</FONT> <BR><FONT color=#0000ff>public</FONT> <FONT color=#0000ff>class</FONT> TestPostXml { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>public</FONT> <FONT color=#0000ff>static</FONT> <FONT color=#0000ff>void</FONT> main(String[] args) <FONT color=#0000ff>throws</FONT> IOException { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpClient httpclient = <FONT color=#0000ff>new</FONT> DefaultHttpClient(); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpPost httppost = <FONT color=#0000ff>new</FONT> HttpPost(<FONT color=#800000>"http:<FONT color=#008000>//localhost:8080/waitsrv/GenXmlServlet"); </FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StringEntity myEntity = <FONT color=#0000ff>new</FONT> StringEntity(<FONT color=#800000>"&lt;html&gt;你好啊啊&lt;/html&gt;"</FONT>, <FONT color=#800000>"GBK"</FONT>); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;httppost.addHeader(<FONT color=#800000>"Content-Type"</FONT>, <FONT color=#800000>"text/xml"</FONT>); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;httppost.setEntity(myEntity); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpResponse response = httpclient.execute(httppost); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpEntity resEntity = response.getEntity(); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InputStreamReader reader = <FONT color=#0000ff>new</FONT> InputStreamReader(resEntity.getContent(), <FONT color=#800000>"ISO-8859-1"</FONT>); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>char</FONT>[] buff = <FONT color=#0000ff>new</FONT> <FONT color=#0000ff>char</FONT>; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>int</FONT> length = 0; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>while</FONT> ((length = reader.read(buff)) != -1) { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<FONT color=#0000ff>new</FONT> String(buff, 0, length)); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;httpclient.getConnectionManager().shutdown(); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <BR>}</DIV></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV>服务端接收方式:</DIV>
<DIV>
<DIV style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 16px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 4px; FONT-FAMILY: verdana, 宋体; COLOR: #000000; FONT-SIZE: 10pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><FONT color=#0000ff>package</FONT> com; <BR><BR><FONT color=#0000ff>import</FONT> javax.servlet.ServletException; <BR><FONT color=#0000ff>import</FONT> javax.servlet.http.HttpServlet; <BR><FONT color=#0000ff>import</FONT> javax.servlet.http.HttpServletRequest; <BR><FONT color=#0000ff>import</FONT> javax.servlet.http.HttpServletResponse; <BR><FONT color=#0000ff>import</FONT> java.io.IOException; <BR><FONT color=#0000ff>import</FONT> java.io.InputStreamReader; <BR><FONT color=#0000ff>import</FONT> java.io.PrintWriter; <BR><BR><FONT color=#008000>/** <BR>* 接收XLM请求 <BR>* <BR>* @author leizhimin 2010-7-8 1:02:42 <BR>*/</FONT> <BR><FONT color=#0000ff>public</FONT> <FONT color=#0000ff>class</FONT> GenXmlServlet <FONT color=#0000ff>extends</FONT> HttpServlet { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>protected</FONT> <FONT color=#0000ff>void</FONT> doPost(HttpServletRequest req, HttpServletResponse resp) <FONT color=#0000ff>throws</FONT> ServletException, IOException { <BR><FONT color=#008000>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String xml = req.getParameter("xmldata"); </FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resp.setContentType(<FONT color=#800000>"text/xml"</FONT>); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resp.setCharacterEncoding(<FONT color=#800000>"GBK"</FONT>); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrintWriter out = resp.getWriter(); <BR><FONT color=#008000>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.println(xml); </FONT><BR><FONT color=#008000>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(xml); </FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<FONT color=#800000>"----------------------"</FONT>); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InputStreamReader reader = <FONT color=#0000ff>new</FONT> InputStreamReader(req.getInputStream(), <FONT color=#800000>"GBK"</FONT>); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>char</FONT>[] buff = <FONT color=#0000ff>new</FONT> <FONT color=#0000ff>char</FONT>; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>int</FONT> length = 0; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>while</FONT> ((length = reader.read(buff)) != -1) { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String x = <FONT color=#0000ff>new</FONT> String(buff, 0, length); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(x); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.print(x); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>protected</FONT> <FONT color=#0000ff>void</FONT> doGet(HttpServletRequest req, HttpServletResponse resp) <FONT color=#0000ff>throws</FONT> ServletException, IOException { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resp.setContentType(<FONT color=#800000>"text/html"</FONT>); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PrintWriter out = resp.getWriter(); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.println(<FONT color=#800000>"&lt;html&gt;"</FONT>); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.println(<FONT color=#800000>"&lt;head&gt;"</FONT>); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.println(<FONT color=#800000>"&lt;title&gt;Hello World!&lt;/title&gt;"</FONT>); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.println(<FONT color=#800000>"&lt;/head&gt;"</FONT>); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.println(<FONT color=#800000>"&lt;body&gt;"</FONT>); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.println(<FONT color=#800000>"&lt;h1&gt;Hello World!!&lt;/h1&gt;"</FONT>); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.println(<FONT color=#800000>"&lt;/body&gt;"</FONT>); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;out.println(<FONT color=#800000>"&lt;/html&gt;"</FONT>); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <BR>}</DIV></DIV>
<DIV>&nbsp;</DIV>
<DIV>web.xml</DIV>
<DIV>
<DIV style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 16px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 4px; FONT-FAMILY: verdana, 宋体; COLOR: #000000; FONT-SIZE: 10pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><FONT color=#0000ff>&lt;?</FONT><FONT color=#800000>xml</FONT> <FONT color=#ff0000>version</FONT><FONT color=#0000ff>="1.0"</FONT> <FONT color=#ff0000>encoding</FONT><FONT color=#0000ff>="UTF-8"</FONT><FONT color=#0000ff>?&gt;</FONT> <BR><FONT color=#0000ff>&lt;</FONT><FONT color=#800000>web-app</FONT> <FONT color=#ff0000>xmlns</FONT><FONT color=#0000ff>="http://java.sun.com/xml/ns/javaee"</FONT> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT color=#ff0000>xmlns:xsi</FONT><FONT color=#0000ff>="http://www.w3.org/2001/XMLSchema-instance"</FONT> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xsi:schemaLocation="http://java.sun.com/xml/ns/javaee <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT color=#ff0000>version</FONT><FONT color=#0000ff>="2.5"</FONT><FONT color=#0000ff>&gt;</FONT> <BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>&lt;</FONT><FONT color=#800000>servlet</FONT><FONT color=#0000ff>&gt;</FONT> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>&lt;</FONT><FONT color=#800000>servlet-name</FONT><FONT color=#0000ff>&gt;</FONT>GenXmlServlet<FONT color=#0000ff>&lt;/</FONT><FONT color=#800000>servlet-name</FONT><FONT color=#0000ff>&gt;</FONT> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>&lt;</FONT><FONT color=#800000>servlet-class</FONT><FONT color=#0000ff>&gt;</FONT>com.GenXmlServlet<FONT color=#0000ff>&lt;/</FONT><FONT color=#800000>servlet-class</FONT><FONT color=#0000ff>&gt;</FONT> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>&lt;/</FONT><FONT color=#800000>servlet</FONT><FONT color=#0000ff>&gt;</FONT> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>&lt;</FONT><FONT color=#800000>servlet-mapping</FONT><FONT color=#0000ff>&gt;</FONT> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>&lt;</FONT><FONT color=#800000>servlet-name</FONT><FONT color=#0000ff>&gt;</FONT>GenXmlServlet<FONT color=#0000ff>&lt;/</FONT><FONT color=#800000>servlet-name</FONT><FONT color=#0000ff>&gt;</FONT> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>&lt;</FONT><FONT color=#800000>url-pattern</FONT><FONT color=#0000ff>&gt;</FONT>/GenXmlServlet<FONT color=#0000ff>&lt;/</FONT><FONT color=#800000>url-pattern</FONT><FONT color=#0000ff>&gt;</FONT> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>&lt;/</FONT><FONT color=#800000>servlet-mapping</FONT><FONT color=#0000ff>&gt;</FONT> <BR><FONT color=#0000ff>&lt;/</FONT><FONT color=#800000>web-app</FONT><FONT color=#0000ff>&gt;</FONT></DIV></DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV>3、在2的基础,上改为单线程重用连接模式</DIV>
<DIV>&nbsp;</DIV>
<DIV>
<DIV>
<DIV style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 16px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 4px; FONT-FAMILY: verdana, 宋体; COLOR: #000000; FONT-SIZE: 10pt; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><FONT color=#0000ff>import</FONT> org.apache.http.HttpEntity; <BR><FONT color=#0000ff>import</FONT> org.apache.http.HttpResponse; <BR><FONT color=#0000ff>import</FONT> org.apache.http.NameValuePair; <BR><FONT color=#0000ff>import</FONT> org.apache.http.client.HttpClient; <BR><FONT color=#0000ff>import</FONT> org.apache.http.client.entity.UrlEncodedFormEntity; <BR><FONT color=#0000ff>import</FONT> org.apache.http.client.methods.HttpPost; <BR><FONT color=#0000ff>import</FONT> org.apache.http.impl.client.DefaultHttpClient; <BR><FONT color=#0000ff>import</FONT> org.apache.http.impl.conn.SingleClientConnManager; <BR><FONT color=#0000ff>import</FONT> org.apache.http.message.BasicNameValuePair; <BR><FONT color=#0000ff>import</FONT> org.apache.http.entity.*; <BR><BR><BR><FONT color=#0000ff>import</FONT> java.io.IOException; <BR><FONT color=#0000ff>import</FONT> java.io.InputStreamReader; <BR><FONT color=#0000ff>import</FONT> java.util.ArrayList; <BR><FONT color=#0000ff>import</FONT> java.util.List; <BR><BR><FONT color=#008000>/** <BR>* 不指定参数名的方式来POST数据,单线程重用连接模式 <BR>* <BR>* @author leizhimin 2010-7-8 3:22:53 <BR>*/</FONT> <BR><FONT color=#0000ff>public</FONT> <FONT color=#0000ff>class</FONT> TestPostXml2 { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>public</FONT> <FONT color=#0000ff>static</FONT> <FONT color=#0000ff>void</FONT> main(String[] args) <FONT color=#0000ff>throws</FONT> IOException { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SingleClientConnManager sccm =<FONT color=#0000ff>new</FONT> SingleClientConnManager(); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpClient httpclient = <FONT color=#0000ff>new</FONT> DefaultHttpClient(sccm); <BR><FONT color=#008000>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpGet httpget = new HttpGet(urisToGet); </FONT><BR><FONT color=#008000>//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpClient httpclient = new DefaultHttpClient(); </FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpPost httppost = <FONT color=#0000ff>new</FONT> HttpPost(<FONT color=#800000>"http:<FONT color=#008000>//localhost:8080/waitsrv/GenXmlServlet"); </FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StringEntity myEntity = <FONT color=#0000ff>new</FONT> StringEntity(<FONT color=#800000>"&lt;html&gt;你好啊啊&lt;/html&gt;"</FONT>, <FONT color=#800000>"GBK"</FONT>); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;httppost.addHeader(<FONT color=#800000>"Content-Type"</FONT>, <FONT color=#800000>"text/xml"</FONT>); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;httppost.setEntity(myEntity); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpResponse response = httpclient.execute(httppost); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HttpEntity resEntity = response.getEntity(); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InputStreamReader reader = <FONT color=#0000ff>new</FONT> InputStreamReader(resEntity.getContent(), <FONT color=#800000>"ISO-8859-1"</FONT>); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>char</FONT>[] buff = <FONT color=#0000ff>new</FONT> <FONT color=#0000ff>char</FONT>; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>int</FONT> length = 0; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT color=#0000ff>while</FONT> ((length = reader.read(buff)) != -1) { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(<FONT color=#0000ff>new</FONT> String(buff, 0, length)); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;httpclient.getConnectionManager().shutdown(); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <BR>}</DIV></FONT></DIV></DIV>
<DIV>&nbsp;</DIV></DIV></DIV>
页: [1]
查看完整版本: HttpClient4 Post XML到一个服务器上