import java.io.IOException;
import java.util.List; import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.io.IOUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import com.eecn.warehouse.api.model.Address;
import com.eecn.warehouse.api.model.User;
import com.eecn.warehouse.utils.FoodBase;
import com.frogsing.common.api.open.storage.hy.TCustomer;
import com.frogsing.common.open.tools.xmlUtils;
import com.google.common.collect.Lists;
import com.thoughtworks.xstream.XStream; @Controller
@RequestMapping(value = {"/api"})
public class ApiAction {
private static Logger logger = LoggerFactory.getLogger(ApiAction.class);
public static final String SERVER_URL = "http://192.168.1.100/api/StorageApi"; @RequestMapping(value = {"/StorageApi"}, method = RequestMethod.POST)
public String storageApi(TCustomer model, HttpServletRequest request, HttpServletResponse response) throws DocumentException {
String xml = request.getParameter("xml");
String signature = request.getParameter("signature");
//************************http client*******************//
PostMethod postMethod = null;
try {
// 构造HttpClient的实例
HttpClient httpClient = new HttpClient();
// 设置连接超时
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
// 设置字符集
httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
// 创建POST方法的实例
postMethod = new PostMethod(SERVER_URL);
// 使用系统提供的默认的恢复策略 该策略在碰到IO异常的时候将自动重试3次。
postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 20000);
// 异常时,重试处理器
postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); //参数
NameValuePair[] nameValuePairs = new NameValuePair[2];
nameValuePairs[0] = new NameValuePair("xml", xml);
nameValuePairs[1] = new NameValuePair("signature", signature); // 将参数加入到请求方法中
postMethod.setRequestBody(nameValuePairs);
// 发送连接
int statusCode = httpClient.executeMethod(postMethod); if (statusCode == HttpStatus.SC_OK) { String result = postMethod.getResponseBodyAsString();
logger.info("返回数据1:" + result);
logger.info("字符集:" + postMethod.getResponseCharSet());
String rspXml = new String(postMethod.getResponseBody(), "UTF-8");
logger.info("返回数据2:" + rspXml); Document document = DocumentHelper.parseText(rspXml); Element signatureNode = (Element)document.selectSingleNode("//signature");
Element dataNode = (Element)document.selectSingleNode("//data"); String signXml = signatureNode.getText();
String dataXml = dataNode.getText(); String pureXml = FoodBase.decodeXml(dataXml); request.setAttribute("pureXml", pureXml);
request.setAttribute("signXml", signXml);
}
} catch (Exception e) {
logger.error("http client invoke error.", e);
} finally {
if (postMethod != null) {
postMethod.releaseConnection();
}
} //******************************************************//
ServletInputStream inputStream = null;
String rspXml = null;
try {
inputStream = request.getInputStream();
rspXml = IOUtils.toString(inputStream, "UTF-8");
} catch (IOException e) {
logger.error("读取交易仓单返回数据错误.", e);
} return "api/storage";
} @RequestMapping(value = {"/MarketApi"}, method = RequestMethod.POST)
public String marketApi(HttpServletRequest request, HttpServletResponse response) { return "api/market";
} public static void main(String[] args) {
XStream xstream = new XStream();
xstream.alias("User", User.class);
xstream.alias("Address", Address.class); //xStream.aliasAttribute(User.class, "name", "name");
xstream.useAttributeFor(User.class, "name"); List<Address> addressList = Lists.newArrayList();
for (int i = 0; i < 3; i++) {
Address address = new Address();
address.setProvince("浙江");
addressList.add(address);
}
User user = new User();
user.setAddressList(addressList); user.setAccount("asddaa");
user.setAge(23);
user.setGender(0);
user.setName("zhang"); System.out.println(xstream.toXML(user)); System.out.println(xmlUtils.toEntityXml(user));
}
}

get方法类似。

使用HttpClient进行http post/get方法的调用,以及使用dom4j解析xml的更多相关文章

  1. java dom4j解析xml用到的几个方法

    1. 读取并解析XML文档: SAXReader reader = new SAXReader(); Document document = reader.read(new File(fileName ...

  2. 四种生成和解析XML文档的方法详解(介绍+优缺点比较+示例)

    众所周知,现在解析XML的方法越来越多,但主流的方法也就四种,即:DOM.SAX.JDOM和DOM4J 下面首先给出这四种方法的jar包下载地址 DOM:在现在的Java JDK里都自带了,在xml- ...

  3. 解析XML的方法

    解析XML的方法 1.DOM生成和解析XML 2.SAX生成和解析XML 3.DOM4J生成和解析XML 4.JDOM生成和解析XML

  4. 四种生成和解析XML文档的方法详解

    众所周知,现在解析XML的方法越来越多,但主流的方法也就四种,即:DOM.SAX.JDOM和DOM4J 下面首先给出这四种方法的jar包下载地址 DOM:在现在的Java JDK里都自带了,在xml- ...

  5. java解析XML文件四种方法之引入源文件

    1.DOM解析(官方) try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();         Documen ...

  6. 大杂烩 -- 四种生成和解析XML文档的方法详解

    基础大杂烩 -- 目录 众所周知,现在解析XML的方法越来越多,但主流的方法也就四种,即:DOM.SAX.JDOM和DOM4J DOM:在现在的Java JDK里都自带了,在xml-apis.jar包 ...

  7. 使用HttpClient 发送get、post请求,及其解析xml返回数据

    一.关于HttpClient的使用: 可以参考这个博客地址,这里有详细的介绍,需要的可以先看一下: 地址是:http://blog.csdn.net/wangpeng047/article/detai ...

  8. Android DOM解析XML方法及优化

    在Android应用开发中,我们常常要在应用启动后从服务器下载一些配置文件,这些配置文件包含一些项目中可能用到的资源,这些文件很多情况下是XML文件,这时就要将XML下载到文件中保存,之后再解析XML ...

  9. UIView的layoutSubviews和drawRect方法何时调用

    首先两个方法都是异步执行.layoutSubviews方便数据计算,drawRect方便视图重绘. layoutSubviews在以下情况下会被调用: 1.init初始化不会触发layoutSubvi ...

随机推荐

  1. Global中的事件执行顺序

    The Global.asax file, sometimes called the ASP.NET application file, provides a way to respond to ap ...

  2. 【NOIP TG 解方程】

    存代码: #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> ...

  3. [jobdu]丑数

    由于思维的惯性,用了queue.后来发现一要注意要用集合判重,二是每次往queue里放的多,后来溢出了,要用long long.但这样要用数组,集合,队列,内存多.效率是O(n*logn)的. #in ...

  4. live555

    相关资料: Live555 是一个为流媒体提供解决方案的跨平台的C++开源项目,它实现了对标准流媒体传输协议如RTP/RTCP.RTSP.SIP等的支持.Live555实现 了对多种音视频编码格式的音 ...

  5. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-009-带参数的ADVICE2 配置文件为XML

    一. 1.配置文件为xml时则切面类不用写aop的anotation package com.springinaction.springidol; public class Magician impl ...

  6. HDU 4288 Coder 【线段树+离线处理+离散化】

    题意略. 离线处理,离散化.然后就是简单的线段树了.需要根据mod 5的值来维护.具体看代码了. /* 线段树+离散化+离线处理 */ #include <cstdio> #include ...

  7. linux 内核驱动--Platform Device和Platform_driver注册过程

    linux 内核驱动--Platform Device和Platform_driver注册过程 从 Linux 2.6 起引入了一套新的驱动管理和注册机制 :Platform_device 和 Pla ...

  8. puTTY与SecureCRT的比较

    从网上看到别人对这两个工具的比较:从windows访问linux,除了samba之外,日常操作用得最多的大概就是PuTTY和SecureCRT Putty是免费的,SecureCRT是收费的(当然,有 ...

  9. HTML页面的导出,包括Excel和Word导出

    //导出到Excel --- 全部导出,可以设置一些隐藏进行导出 protected void btnExport_Click(object sender, EventArgs e)    {     ...

  10. EasyUI中datagrid的行编辑模式中,找到特定的Editor,并为其添加事件

    有时候在行编辑的时候,一个编辑框的值要根据其它编辑框的值进行变化,那么可以通过在开启编辑时,找到特定的Editor,为其添加事件 // 绑定事件, index为当前编辑行 var editors = ...