通过http请求传递xml流和接收xml流的代码示例
通过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流的代码示例的更多相关文章
- 使用postman进行post请求传递中文导致后台接收乱码的问题
1.个人猜测估计是如果header里不指明编码的话,经过tomcat服务器时会导致转换乱码信息,这样就算你在filter里配置了EncodingFilter相关的过滤器也无济于事.. 解决方法就是在h ...
- rabbitmq AmqpClient 使用Direct 交换机投递与接收消息,C++代码示例
// 以DIRECT 交换机和ROUTING_KEY的方式进行消息的发布与订阅 // send // strUri = "amqp://guest:guest@192.168.30.11:8 ...
- rabbitmq AmqpClient 使用Topic 交换机投递与接收消息,C++代码示例
// strUri = "amqp://guest:guest@192.168.30.11:8820/test" // strUri = "amqp://[帐户名]:[密 ...
- rabbitmq AmqpClient 使用Fanout 交换机投递与接收消息,C++代码示例
fanout交换器重点内容非常简单.它只会将接收到的所有消息广播发送到它所知道的所有队列. 投递消息到交换机: #include "SimpleAmqpClient/SimpleAmqpCl ...
- .net webapi 接收 xml 格式数据的三种情况
webapi 接收 xml 的三种方法 前段时间接到一个任务写一个小接口,要接收java端返回过来的短信xml数据. 刚拿到项目,我的第一想法是对方会以什么形式发送xml格式的数据给我呢,设想三种情况 ...
- 【struts2】struts2中的流接收与流发送
[前言]在我们的struts2后端中,实现流的接收和发送.就能够实现向server传视频流以及下载图片. [流接收] 如今举一个传公钥的样例.struts2用一个action接收Key,而Key就是用 ...
- PHP通过XML报文格式的POST请求方式,与第三方接口交互(发送xml,获取XML,并解析xml步骤)
开发者端:发送请求,并接收结果 <?php // 下面的demo,实现的功能如下: // 1-开发者需要判断一个用户是否存在,去请求第三方接口. // 2-与第三方接口的通信,是以xml格式传送 ...
- Ajax请求传递参数遇到的问题
想写个同类型的,代码未测. 什么是WebAPI?我的理解是WebAPI+JQuery(前端)基本上能完成Web MVC的功能,即:这么理解吧,WebAPI相当于Web MVC的后台部分. 接下来直接上 ...
- WebAPI学习日记一:Ajax请求传递参数遇到的问题
首先,本人大学刚毕业,想把自己学习的一些东西记录下来,也是和大家分享,如有不对之处还请多加指正.声明:但凡是我博客里的文章均是本人实际操作遇到的例子,不会随便从网上拷贝或者转载,本着对自己和观众负责的 ...
随机推荐
- LoadRunner中log的使用总结
LoadRunner中log的使用总结 1.log的设置方式. 在 runtime setting中可以设置log的生成方式: 默认的log方式: Enable logging选中,log optio ...
- Ubuntu16.04下Kylin的安装与配置
一.系统环境 kylin的安装配置并不像官方文档中描述的那样简单,复杂的原因在于hadoop,hive,hbase,kylin的版本一定要兼容,不然就会出现各种奇怪的错误.以下各软件版本可以成功运行k ...
- Mongodb C#客户端数据关联数据,使用Linq语法进行关联
在Mongodb C# drivers 文档 官方地址:https://docs.mongodb.com/ecosystem/drivers/csharp/ 基础的使用请参考<c# Mongod ...
- Python处理海量数据的实战研究
最近看了July的一些关于Java处理海量数据的问题研究,深有感触,链接:http://blog.csdn.net/v_july_v/article/details/6685962 感谢July ^_ ...
- 联系表单 1_copy
你的名字 (必填) [text* your-name] 你的邮箱 (必填) [email* your-email] 主题 [text your-subject] 你的留言 [textarea your ...
- Winform 串口通讯之地磅
继上次的读卡之后,要做一个地磅的读取. 下面是我在读卡Demo上改的读取地磅的. 地磅是一直向串口发送数据的,所以需要截取数据来一直判断数据是否合法,然后计算出结果. 其中遇到了一个小问题,文末有介绍 ...
- 02-c#基础之01-基础语法(一)
1.注释符 1)注销 2) 解释 2.C#中的3种注释符 1)单行注释// 2)多行注释/*要注释的内容*/ 3)文档注释///多用来解释类或者方法 2.VS中的快捷键
- 排序算法之冒泡排序Java实现
排序算法之冒泡排序 舞蹈演示排序: 冒泡排序: http://t.cn/hrf58M 希尔排序:http://t.cn/hrosvb 选择排序:http://t.cn/hros6e 插入排序:ht ...
- 20162318 2018-2019-2《网络对抗技术》Exp0 Kali安装 Week1
1.配置虚拟机 参考博客链接 2.安装kali与配置网络 参考博客链接 3.配置共享文件夹 参考博客链接 4.更换软件源 参考博客链接
- java知识点总结
一.java 1.容器 1)List Java中ArrayList和LinkedList区别 2)Set 理解HashSet及使用 HashMap和HashSet的区别 3Map HashMap的容量 ...