Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6643032
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: 系统运维

2011-05-17 09:51:34

原创作品,允许转载,转载时请务必以超链接形式标明文章 、作者信息和本声明。否则将追究法律责任。http://lavasoft.blog.51cto.com/62575/347157
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 formparams = new ArrayList();
                formparams.add(new BasicNameValuePair("xmldate", "你好啊啊"));
                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("你好啊啊", "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("");
                out.println("");
                out.println("Hello World!");
                out.println("");
                out.println("");
                out.println("

Hello World!!

"
);
                out.println(" ");
                out.println("");
        }
}
 
web.xml
xml version="1.0" encoding="UTF-8"?>
<web-app xmlns=""
                 xmlns:xsi=""
                 xsi:schemaLocation="
        /web-app_2_5.xsd"
                 version="2.5">

        <servlet>
                <servlet-name>GenXmlServletservlet-name>
                <servlet-class>com.GenXmlServletservlet-class>
        servlet>
        <servlet-mapping>
                <servlet-name>GenXmlServletservlet-name>
                <url-pattern>/GenXmlServleturl-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("你好啊啊", "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();
        }
}
 
阅读(1798) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~