`
m635674608
  • 浏览: 4929185 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

HttpClient4 Post XML到一个服务器上

 
阅读更多
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(); 
        } 
}
 
 
 
 

本文出自 “熔 岩” 博客,请务必保留此出处http://lavasoft.blog.51cto.com/62575/347157

分享到:
评论

相关推荐

    httpclient 客户端发送xml报文到服务器端采用post方式(加密)传递

    采用httpclient发送xml报文,httpServer接收报文。两个程序一看就明白可以直接调用执行不在多说。

    HttpClient通过Post上传文件的实例代码

    但是项目中涉及到既要传递普通参数,也要传递多个文件(不是单纯的传递XML文件)。在网上寻找之后,发现是使用HttClient来进行响应的操作,起初尝试多次依然不能传递参数和传递文件,后来发现时因为当使用HttpClient...

    httpclient开发包

    httpclient模拟浏览器中一个表单提交或者说异步提交的方法,返回通常为json或xml的格式,可以采用POST和GET传送并设置字符集以及超时控制。 URL暴露方式双方约定认证协议方法即可,另一种是获取cookie方式,但是...

    黎活明android教程的全程PPT

    使用HttpClient开源项目提交参数给服务器 4&gt; 网络--通过HTTP协议实现文件上传 第五天 1&gt; 网络--通过HTTP协议发送XML数据,并调用webservice实现手机号归属地查询 2&gt; 网络--通过HTTP协议实现多线程断点续传下载 3...

    传智播客Android视频教程-课程源码.rar

    4&gt; 开发与运行(卸载)第一个ANDROID应用 5&gt; 项目的目录结构 6&gt; 项目清单文件分析 7&gt; 分析第一个ANDROID应用的启动过程 8&gt; 电话拔打 9&gt; 查看手机模拟器往控制台输出的日志信息 10&gt; 如何部署应用到真实手机 11&gt; 短信...

    hadoop中文文档

    4.1 ARC Writer Processor 这是一个把捉取到的结果进行写写的处理,存储为 ARC(网络档案文件格式)的文档。每个 Heritrix实例只有一个这样的些处理线程在运行。 4.2 Default Metadata Provider 一个合适爬虫元数据...

    Android开发资料合集--续

    将图片保存到SD卡上 50 26、TextView垂直滚动 51 27、判断某服务是否开启 56 28、判断SD卡是否已挂载 56 29、文件操作类 57 1、获得文件或目录的大小 57 2、递归删除目录或文件 57 30、手动更新所有Widget 58 31、...

    OPhone应用开发权威指南(黄晓庆)

    2.2 第一个OPhone应用程序 16 2.2.1 新建OPhone项目 16 2.2.2 运行OPhone项目 18 2.2.3 更新资源文件 21 2.3 调试OPhone应用程序 24 2.3.1 设置断点 25 2.3.2 启动调试 25 2.3.3 单步跟踪 26 2.4 在命令行下开发...

    Android开发案例驱动教程 配套代码

    注: 由于第12,13,14章代码太大,无法上传到一个包中。 这三节代码会放到其他压缩包中。 作者:关东升,赵志荣 Java或C++程序员转变成为Android程序员 采用案例驱动模式展开讲解知识点,即介绍案例-&gt;案例涉及...

    黑马程序员 安卓学院 万元哥项目经理 分享220个代码实例

    |--TabHost一个界面显示多Activity |--TextView单行跑马灯效果 |--TextView虚拟获得焦点 |--uploadServlet |--uri之表示资源resource |--ViewPage的使用 |--view中的tag用法之存储对象 |--view常用属性 |--xml常用...

Global site tag (gtag.js) - Google Analytics