Calling interfaces over HTTP links

Source: Internet
Author: User
Tags soap

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 ("&lt;", "<"). Replace ("&gt;", ">"). 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.