HttpClient4 Post XML到一个服务器上

简介:
现在网上介绍的HttpClient基本上全是3.x版本的内容,HttpClient4的API变化相对3已经变化很大,对HttpClient4做了简单的研究后,完成了一个HttpClient4 Post XML功能。
 
对于POST方式,最先想到的就是表单提交了,POST XML自然想到的就是定义一个变量名,比如叫xmldata,然后将这个参数的值POST出去,在服务端接收的时候,自然也是通过 requset.getParameter("xmldata")方式来接收。
 
现在我在这里要做的不是通过上面的方式,而是不指定参数名来Post,实际上就是将一个流写入请求。
 
下面是具体的实现方式:
 
1、参数名方式POST XML数据
import org.apache.http.*; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.apache.http.client.*; 

import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 
import java.util.*; 

/** 
* 通过指定参数名的方式POST XML 

* @author leizhimin 2010-7-8 22:29:28 
*/
 
public  class TestPost { 
         public  static  void main(String[] args)  throws IOException { 
                HttpClient httpclient =  new DefaultHttpClient(); 
                HttpPost httppost =  new HttpPost( "http://localhost:8080/waitsrv/GenXmlServlet"); 

                List<NameValuePair> formparams = new ArrayList<NameValuePair>(); 
                formparams.add(new BasicNameValuePair("xmldate""<html>你好啊啊</html>")); 
                formparams.add(new BasicNameValuePair("info""xxxxxxxxx")); 
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "GBK"); 
//                entity.setContentType("text/xml; charset=GBK"); 
                httppost.setEntity(entity); 
                HttpResponse response = httpclient.execute(httppost); 
                HttpEntity resEntity = response.getEntity(); 
                InputStreamReader reader = new InputStreamReader(resEntity.getContent(), "ISO-8859-1"); 
                char[] buff = new char[1024]; 
                int length = 0; 
                while ((length = reader.read(buff)) != -1) { 
                        System.out.println(new String(buff, 0, length)); 
                        httpclient.getConnectionManager().shutdown(); 
                } 
        } 
}
 
2、不指定参数名的方式来POST数据
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.apache.http.entity.*; 


import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.List; 

/** 
* 不指定参数名的方式来POST数据 

* @author leizhimin 2010-7-8 3:22:53 
*/
 
public  class TestPostXml { 
         public  static  void main(String[] args)  throws IOException { 
                HttpClient httpclient =  new DefaultHttpClient(); 
                HttpPost httppost =  new HttpPost( "http://localhost:8080/waitsrv/GenXmlServlet"); 
                StringEntity myEntity = new StringEntity("<html>你好啊啊</html>""GBK"); 
                httppost.addHeader("Content-Type""text/xml"); 
                httppost.setEntity(myEntity); 
                HttpResponse response = httpclient.execute(httppost); 
                HttpEntity resEntity = response.getEntity(); 
                InputStreamReader reader = new InputStreamReader(resEntity.getContent(), "ISO-8859-1"); 
                char[] buff = new char[1024]; 
                int length = 0; 
                while ((length = reader.read(buff)) != -1) { 
                        System.out.println(new String(buff, 0, length)); 
                } 
                httpclient.getConnectionManager().shutdown(); 
        } 
}
 
服务端接收方式:
package com; 

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 

/** 
* 接收XLM请求 

* @author leizhimin 2010-7-8 1:02:42 
*/
 
public  class GenXmlServlet  extends HttpServlet { 
         protected  void doPost(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException { 
//                String xml = req.getParameter("xmldata"); 
                resp.setContentType( "text/xml"); 
                resp.setCharacterEncoding( "GBK"); 
                PrintWriter out = resp.getWriter(); 
//                out.println(xml); 
//                System.out.println(xml); 
                System.out.println( "----------------------"); 
                InputStreamReader reader =  new InputStreamReader(req.getInputStream(),  "GBK"); 
                 char[] buff =  new  char[1024]; 
                 int length = 0; 
                 while ((length = reader.read(buff)) != -1) { 
                        String x =  new String(buff, 0, length); 
                        System.out.println(x); 
                        out.print(x); 
                } 
        } 

         protected  void doGet(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException { 
                resp.setContentType( "text/html"); 
                PrintWriter out = resp.getWriter(); 
                out.println( "<html>"); 
                out.println( "<head>"); 
                out.println( "<title>Hello World!</title>"); 
                out.println( "</head>"); 
                out.println( "<body>"); 
                out.println( "<h1>Hello World!!</h1>"); 
                out.println( "</body>"); 
                out.println( "</html>"); 
        } 
}
 
web.xml
<? xml  version ="1.0"  encoding ="UTF-8" ?> 
< web-app  xmlns ="http://java.sun.com/xml/ns/javaee" 
                  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" 
                 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
                  version ="2.5" > 

         < servlet > 
                 < servlet-name >GenXmlServlet </ servlet-name > 
                 < servlet-class >com.GenXmlServlet </ servlet-class > 
         </ servlet > 
         < servlet-mapping > 
                 < servlet-name >GenXmlServlet </ servlet-name > 
                 < url-pattern >/GenXmlServlet </ url-pattern > 
         </ servlet-mapping > 
</ web-app >
 
 
3、在2的基础,上改为单线程重用连接模式
 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.impl.conn.SingleClientConnManager; 
import org.apache.http.message.BasicNameValuePair; 
import org.apache.http.entity.*; 


import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.List; 

/** 
* 不指定参数名的方式来POST数据,单线程重用连接模式 

* @author leizhimin 2010-7-8 3:22:53 
*/
 
public  class TestPostXml2 { 
         public  static  void main(String[] args)  throws IOException { 
                SingleClientConnManager sccm = new SingleClientConnManager(); 
                HttpClient httpclient =  new DefaultHttpClient(sccm); 
//                HttpGet httpget = new HttpGet(urisToGet[i]); 
//                HttpClient httpclient = new DefaultHttpClient(); 
                HttpPost httppost =  new HttpPost( "http://localhost:8080/waitsrv/GenXmlServlet"); 
                StringEntity myEntity = new StringEntity("<html>你好啊啊</html>""GBK"); 
                httppost.addHeader("Content-Type""text/xml"); 
                httppost.setEntity(myEntity); 
                HttpResponse response = httpclient.execute(httppost); 
                HttpEntity resEntity = response.getEntity(); 
                InputStreamReader reader = new InputStreamReader(resEntity.getContent(), "ISO-8859-1"); 
                char[] buff = new char[1024]; 
                int length = 0; 
                while ((length = reader.read(buff)) != -1) { 
                        System.out.println(new String(buff, 0, length)); 
                } 
                httpclient.getConnectionManager().shutdown(); 
        } 
}
 
 
 
 本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/347157,如需转载请自行联系原作者
相关文章
|
XML 前端开发 测试技术
如何使用 Postman 发送 POST XML 请求?
使用 Postman 发送带有 XML 数据的 POST 请求。我们将引导您完成将 XML 数据有效发送到 Web 服务或 API 的步骤,使处理这种常见数据格式变得简单易行。
|
XML 分布式计算 资源调度
大数据-02-Hadoop集群 XML配置 超详细 core-site.xml hdfs-site.xml 3节点云服务器 2C4G HDFS Yarn MapRedece(一)
大数据-02-Hadoop集群 XML配置 超详细 core-site.xml hdfs-site.xml 3节点云服务器 2C4G HDFS Yarn MapRedece(一)
666 5
|
XML 资源调度 网络协议
大数据-02-Hadoop集群 XML配置 超详细 core-site.xml hdfs-site.xml 3节点云服务器 2C4G HDFS Yarn MapRedece(二)
大数据-02-Hadoop集群 XML配置 超详细 core-site.xml hdfs-site.xml 3节点云服务器 2C4G HDFS Yarn MapRedece(二)
749 5
|
分布式计算 资源调度 Hadoop
大数据-01-基础环境搭建 超详细 Hadoop Java 环境变量 3节点云服务器 2C4G XML 集群配置 HDFS Yarn MapRedece
大数据-01-基础环境搭建 超详细 Hadoop Java 环境变量 3节点云服务器 2C4G XML 集群配置 HDFS Yarn MapRedece
479 4
|
XML 开发框架 .NET
服务器上的 XML
服务器上的 XML
|
前端开发 JavaScript
【node写接口】 通过node 快速搭建一个服务器、get请求、post请求 小白入门
【node写接口】 通过node 快速搭建一个服务器、get请求、post请求 小白入门
906 4
|
8月前
|
弹性计算 运维 安全
阿里云轻量应用服务器与云服务器ECS啥区别?新手帮助教程
阿里云轻量应用服务器适合个人开发者搭建博客、测试环境等低流量场景,操作简单、成本低;ECS适用于企业级高负载业务,功能强大、灵活可扩展。二者在性能、网络、镜像及运维管理上差异显著,用户应根据实际需求选择。
730 10
|
8月前
|
弹性计算 ice
阿里云4核8g服务器多少钱一年?1个月和1小时价格,省钱购买方法分享
阿里云4核8G服务器价格因实例类型而异,经济型e实例约159元/月,计算型c9i约371元/月,按小时计费最低0.45元。实际购买享折扣,1年最高可省至1578元,附主流ECS实例及CPU型号参考。
811 8
|
8月前
|
运维 安全 Ubuntu
阿里云渠道商:服务器操作系统怎么选?
阿里云提供丰富操作系统镜像,涵盖Windows与主流Linux发行版。选型需综合技术兼容性、运维成本、安全稳定等因素。推荐Alibaba Cloud Linux、Ubuntu等用于Web与容器场景,Windows Server支撑.NET应用。建议优先选用LTS版本并进行测试验证,通过标准化镜像管理提升部署效率与一致性。
|
8月前
|
存储 监控 安全
阿里云渠道商:云服务器价格有什么变动?
阿里云带宽与存储费用呈基础资源降价、增值服务差异化趋势。企业应结合业务特点,通过阶梯计价、智能分层、弹性带宽等策略优化成本,借助云监控与预算预警机制,实现高效、可控的云资源管理。