通过http请求传递xml流和接收xml流的代码示例

//1.在servlet中post一个xml流:
import java.io.OutputStreamWriter;
import org.jdom.Document;
import org.jdom.Document;

public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
//do somthing...
response.setContentType("application/xml");
Document doc = createDoc();
    String strdoc = new XMLOutputter().outputString(doc);
    OutputStreamWriter out = new OutputStreamWriter(response
      .getOutputStream(), "UTF-8");
    out.write(strdoc);
    out.flush();
    out.close();
//do somthing...
}

/***
*将要封装的数据,封装为xml文件的Document格式
*/
public Document createDoc() {
   Document doc = null;
   Element root;
   Element viewentry;
   Element entry;
   List list = getData();
   try {
    XMLOutputter docWriter = new XMLOutputter(" ", true);
    docWriter.setEncoding("UTF-8");
    root = new Element("your_element_name");
    doc = new Document(root);
    root = doc.getRootElement();
    if (list == null || list.size() == 0) {
     return doc;
    }
    Iterator it = list.iterator();
    while (it.hasNext()) {
     Map colMap = (Map) it.next();

viewentry = new Element("document");
     entry = new Element("author");
     entry.setText(colMap.get("userid").toString());
     viewentry.addContent(entry);

//do other entry in this way
     root.addContent(viewentry);
    }
   } catch (Exception e) {
    e.printStackTrace();
   }
   return doc;
}

//2.根据接收的url,从其中获取xml流生成xml文件
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

import sun.net.www.protocol.http.HttpURLConnection;

public static Document getDocument(String url){
  
   Document doc = null;
   HttpURLConnection conn = null;
   InputStream ins = null;
   SAXReader reader = null;
   try{
    HttpTimeoutHandler hth = new HttpTimeoutHandler(600000);
    URL conURL = new URL(null,url,hth);
    conn = (HttpURLConnection)conURL.openConnection();
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setUseCaches(false);
    ins = conn.getInputStream();
    reader =new SAXReader();
    doc= reader.read(ins);
    ins.close();
    conn.disconnect();
   }catch (MalformedURLException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();   
   } catch (DocumentException e) {
    e.printStackTrace();
   }catch(Exception e){
    e.printStackTrace();
   }finally {
    try {
     if (ins != null) {
      ins.close();
      ins = null;
     }
    } catch (IOException e1) {
     e1.printStackTrace();
    }
    try {
     if (conn != null) {
      conn.disconnect();
      conn = null;
     }
    } catch (Exception e2) {
     e2.printStackTrace();
    }
   }
   return doc;
}

3.//处理url超时限制
/***
HttpTimeoutHandler.java
*/
import java.net.*;
import java.io.IOException;

public class HttpTimeoutHandler extends sun.net.www.protocol.http.Handler {
int intTimeoutVal;
HttpURLConnectionTimeout fHUCT;

public HttpTimeoutHandler(int iT) {
   intTimeoutVal = iT;
}

protected java.net.URLConnection openConnection(URL u) throws IOException {
   return fHUCT = new HttpURLConnectionTimeout(u, this, intTimeoutVal);
}

String GetProxy() {
   return proxy;
} // breaking encapsulation

int GetProxyPort() {
   return proxyPort;
} // breaking encapsulation

public void Close() throws Exception {
   fHUCT.Close();
}

public Socket GetSocket() {
   return fHUCT.GetSocket();
}
}

通过http请求传递xml流和接收xml流的代码示例的更多相关文章

  1. 使用postman进行post请求传递中文导致后台接收乱码的问题

    1.个人猜测估计是如果header里不指明编码的话,经过tomcat服务器时会导致转换乱码信息,这样就算你在filter里配置了EncodingFilter相关的过滤器也无济于事.. 解决方法就是在h ...

  2. rabbitmq AmqpClient 使用Direct 交换机投递与接收消息,C++代码示例

    // 以DIRECT 交换机和ROUTING_KEY的方式进行消息的发布与订阅 // send // strUri = "amqp://guest:guest@192.168.30.11:8 ...

  3. rabbitmq AmqpClient 使用Topic 交换机投递与接收消息,C++代码示例

    // strUri = "amqp://guest:guest@192.168.30.11:8820/test" // strUri = "amqp://[帐户名]:[密 ...

  4. rabbitmq AmqpClient 使用Fanout 交换机投递与接收消息,C++代码示例

    fanout交换器重点内容非常简单.它只会将接收到的所有消息广播发送到它所知道的所有队列. 投递消息到交换机: #include "SimpleAmqpClient/SimpleAmqpCl ...

  5. .net webapi 接收 xml 格式数据的三种情况

    webapi 接收 xml 的三种方法 前段时间接到一个任务写一个小接口,要接收java端返回过来的短信xml数据. 刚拿到项目,我的第一想法是对方会以什么形式发送xml格式的数据给我呢,设想三种情况 ...

  6. 【struts2】struts2中的流接收与流发送

    [前言]在我们的struts2后端中,实现流的接收和发送.就能够实现向server传视频流以及下载图片. [流接收] 如今举一个传公钥的样例.struts2用一个action接收Key,而Key就是用 ...

  7. PHP通过XML报文格式的POST请求方式,与第三方接口交互(发送xml,获取XML,并解析xml步骤)

    开发者端:发送请求,并接收结果 <?php // 下面的demo,实现的功能如下: // 1-开发者需要判断一个用户是否存在,去请求第三方接口. // 2-与第三方接口的通信,是以xml格式传送 ...

  8. Ajax请求传递参数遇到的问题

    想写个同类型的,代码未测. 什么是WebAPI?我的理解是WebAPI+JQuery(前端)基本上能完成Web MVC的功能,即:这么理解吧,WebAPI相当于Web MVC的后台部分. 接下来直接上 ...

  9. WebAPI学习日记一:Ajax请求传递参数遇到的问题

    首先,本人大学刚毕业,想把自己学习的一些东西记录下来,也是和大家分享,如有不对之处还请多加指正.声明:但凡是我博客里的文章均是本人实际操作遇到的例子,不会随便从网上拷贝或者转载,本着对自己和观众负责的 ...

随机推荐

  1. hdu 5475 模拟计算器乘除 (2015上海网赛H题 线段树)

    给出有多少次操作 和MOD 初始值为1 操作1 y 表示乘上y操作2 y 表示除以第 y次操作乘的那个数 线段树的叶子结点i 表示 第i次操作乘的数 将1替换成y遇到操作2 就把第i个结点的值 替换成 ...

  2. Java第三阶段学习(二、IO流--------递归,字节流Stream)

    一.递归 定义:指方法在方法内调用自己 适用于方法的运算主体不变,但运行的时候,参与运算的方法参数会变化注意:一定要给递归一个出口,否则内存溢出 练习题1:使用递归打印文件夹中所有的文件,包含子目录中 ...

  3. 数据迁移之Sqoop

    一 简介 Apache Sqoop(TM)是一种用于在Apache Hadoop和结构化数据存储(如关系数据库)之间高效传输批量数据的工具 . 官方下载地址:http://www.apache.org ...

  4. 初始Winsock编程

    1.套接字的创建和关闭 使用套接字之前,必须使用socket函数创建一个套接字,此函数调用成功将返回一个套接字句柄. 1 SOCKET socket( 2 int af, //用来指定套接字使用的地址 ...

  5. Java去重字符串的两种方法以及java中冒号的使用

    package com.removesamestring; import java.io.BufferedWriter; import java.util.ArrayList; import java ...

  6. Python学习之字符串格式化

    Table 3.1. 字符串格式化代码 格式 描述 %% 百分号标记% %c 字符及其ASCII码 %s 字符串 %d 有符号整数(十进制) %u 无符号整数(十进制) %o 无符号整数(八进制) % ...

  7. MySQL 大数据量使用limit分页,随着页码的增大,查询效率越低下。

    数据表结构 CREATE TABLE `ad_keyword` ( `id` int(11) NOT NULL AUTO_INCREMENT, `plan_goods_id` int(11) DEFA ...

  8. ASP.NET MVC中在Action获取提交的表单数据方法

    有Index视图如下: 视图代码如下: <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Mas ...

  9. Hadoop整理二(Hadoop分布式存储系统HDFS)

    一.背景 当数据集的大小超过一台独立物理计算机的存储能力时,就有必要对它进行分区(partition) 并存储到若干台单独的计算机上.管理网络中跨多台计算机存储的文件系统称为分布式文件系统 (dist ...

  10. 洛谷P3521 [POI2011]ROT-Tree Rotation [线段树合并]

    题目传送门 Tree Rotation 题目描述 Byteasar the gardener is growing a rare tree called Rotatus Informatikus. I ...