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.Nameva luePair;
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());
//参数
Nameva luePair[] nameva luePairs = new Nameva luePair[2];
nameva luePairs[0] = new Nameva luePair("xml", xml);
nameva luePairs[1] = new Nameva luePair("signature", signature);
// 将参数加入到请求方法中
postMethod.setRequestBody(nameva luePairs);
// 发送连接
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:" + rs