码迷,mamicode.com
首页 > Web开发 > 详细

通过http链接调用接口

时间:2015-01-15 17:41:40      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

依赖包httpclient-4.0.1.jar;xstream-1.3.jar

 

import java.io.IOException;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.client.params.ClientPNames;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.protocol.HTTP;

import org.apache.http.util.EntityUtils;

import org.apache.log4j.Logger;

 

public class HttpClientSoap {

 

    public static final Logger LOG = Logger.getLogger(HttpClientSoap.class);

 

    public static DefaultHttpClient http;

    public static HttpPost post;

    private String url;

 

    public void setUrl(String url) {

this.url = url;

    }

 

    public String request(String entity) {

String resString = null;

if (http == null) {

    http = new DefaultHttpClient();

    post = new HttpPost(url);

    http.getParams().setParameter(

    ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);

}

synchronized (http) {

    HttpEntity en = null;

    try {

HttpEntity re = new StringEntity(entity, HTTP.UTF_8);

post.setHeader("Content-Type", "text/xml; charset=utf-8");

post.setEntity(re);

HttpResponse res = http.execute(post);

if (res != null) {

    en = res.getEntity();

    resString = EntityUtils.toString(en);

}

return resString;

    } catch (Exception e) {

LOG.info("http client error------->" + e);

    } finally {

try {

    en.consumeContent();

} catch (IOException e) {

    LOG.info("can‘t close httpclient-------->" + e);

}

    }

}

return null;

    }

}

/**
* 组建webservice参数.
*/

public class BuildWsParamUtil {
/**
* 调用接口.
* @param methodName 方法名
* @param requestCore 请求参数内容
* @return 完整的xml请求
*/

    public static String xmlRequest(String methodName, String requestCore) {

StringBuffer xml = new StringBuffer("");

xml.append("");

return xml.toString();
}

 

//调用

HttpClientSoap soap = new HttpClientSoap();
soap.setUrl(his_ws_url);
String result = XmlUtil.pareXmlContent(
soap.request(BuildWsParamUtil.XmlRequest("方法名", param)).replace(" xsi:type=\"xsd:string\"", ""), "return");
result = result.replace("&lt;", "<").replace("&gt;", ">").replace("<![CDATA[", "").replace("]]>", "");
XStream xStream = new XStream();
xStream.alias("MessageResult", PatientDiagInfo.class);
PatientDiagInfo patientDiagInfo = (PatientDiagInfo) xStream.fromXML(XmlUtil.pareXml(result, "MessageResult"));

通过http链接调用接口

标签:

原文地址:http://www.cnblogs.com/pharaohyun/p/4226654.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!