Dependency Package : 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;
}
}
/**
* Build WebService parameters.
*/
public class Buildwsparamutil {
/**
* Call interface.
* @param methodname Method name
* @param requestcore Request parameter contents
* @return a complete XML request
*/
public static string XMLRequest (String methodName, String requestcore) {
StringBuffer XML = new StringBuffer ("");
Xml.append ("");
return xml.tostring ();
}
Call
Httpclientsoap soap = new Httpclientsoap ();
Soap.seturl (His_ws_url);
String result = Xmlutil.parexmlcontent (
Soap.request (Buildwsparamutil.xmlrequest ("Method name", 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"));
Calling interfaces over HTTP links