博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Post XML到一个服务器上
阅读量:7058 次
发布时间:2019-06-28

本文共 6271 字,大约阅读时间需要 20 分钟。

  hot3.png

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
*
*  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数据
*
*  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请求
*
*  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数据,单线程重用连接模式
*
*  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();
       }
}

转载于:https://my.oschina.net/zjds/blog/193510

你可能感兴趣的文章
我的友情链接
查看>>
getopt
查看>>
sipp-3.3 使用
查看>>
Hasor 的技术选型
查看>>
Golang HTTPS
查看>>
有效沟通绝技
查看>>
Socket的3次握手链接与4次断开握手
查看>>
PacketInterceptor的妙用
查看>>
FastDFS分布式文件系统——1.安装
查看>>
mysql在windows下的安装教程
查看>>
微软勉强地向Linux作出了用户妥协
查看>>
Twitter的RPC框架Finagle简介
查看>>
时速企业邮箱告诉您搭建企业邮箱服务器需要哪些配置?
查看>>
OAuth的机制原理讲解及开发流程
查看>>
微积分导论--Limit
查看>>
使用PHP Socket 编程模拟Http post和get请求
查看>>
memcached的使用-----php中的常规操作
查看>>
Myeclipse中字符编码的统一设置(以utf-8为例)
查看>>
C++入门学习——虚函数表介绍
查看>>
事件冒泡
查看>>