依赖包: 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("<", "<").replace(">", ">").replace("<![CDATA[", "").replace("]]>", "");
XStream xStream = new XStream();
xStream.alias("MessageResult", PatientDiagInfo.class);
PatientDiagInfo patientDiagInfo = (PatientDiagInfo) xStream.fromXML(XmlUtil.pareXml(result, "MessageResult"));