Java class to call a SOAP - Web service -
i have soap need call oracle , have heard way work out through java class , unfortunately im not familiar java i'm oracle developer ( oracle forms ) appreciated if can me creating class calling soap can build on oracle database , call oracle forms builder way call function .
there 2 soaps (1.1 nd 1.2 ) , both of can work :
* soap 1.1
the following sample soap 1.1 request , response. placeholders shown need replaced actual values.
post /gmgwebservice/service.asmx http/1.1 host: 212.35.66.180 content-type: text/xml; charset=utf-8 content-length: length soapaction: "http://tempuri.org/sendsms" <?xml version="1.0" encoding="utf-8"?> <soap:envelope xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:body> <sendsms xmlns="http://tempuri.org/"> <username>string</username> <password>string</password> <messagebody>string</messagebody> <sender>string</sender> <destination>string</destination> </sendsms> </soap:body> </soap:envelope> http/1.1 200 ok content-type: text/xml; charset=utf-8 content-length: length <?xml version="1.0" encoding="utf-8"?> <soap:envelope xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:body> <sendsmsresponse xmlns="http://tempuri.org/"> <sendsmsresult>string</sendsmsresult> </sendsmsresponse> </soap:body> </soap:envelope>
**soap 1.2
the following sample soap 1.2 request , response. placeholders shown need replaced actual values.
post /gmgwebservice/service.asmx http/1.1 host: 212.35.66.180 content-type: application/soap+xml; charset=utf-8 content-length: length <?xml version="1.0" encoding="utf-8"?> <soap12:envelope xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:body> <sendsms xmlns="http://tempuri.org/"> <username>string</username> <password>string</password> <messagebody>string</messagebody> <sender>string</sender> <destination>string</destination> </sendsms> </soap12:body> </soap12:envelope> http/1.1 200 ok content-type: application/soap+xml; charset=utf-8 content-length: length <?xml version="1.0" encoding="utf-8"?> <soap12:envelope xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:body> <sendsmsresponse xmlns="http://tempuri.org/"> <sendsmsresult>string</sendsmsresult> </sendsmsresponse> </soap12:body> </soap12:envelope>
to implement simple soap clients in java, can use saaj framework (it shipped jse 1.6 , above):
soap attachments api java (saaj) used dealing directly soap request/response messages happens behind scenes in web service api. allows developers directly send , receive soap messages instead of using jax-ws.
see below working example (run it!) of soap web service call using saaj. calls this web service.
import javax.xml.soap.*; import javax.xml.transform.*; import javax.xml.transform.stream.*; public class soapclientsaaj { /** * starting point saaj - soap client testing */ public static void main(string args[]) { try { // create soap connection soapconnectionfactory soapconnectionfactory = soapconnectionfactory.newinstance(); soapconnection soapconnection = soapconnectionfactory.createconnection(); // send soap message soap server string url = "http://ws.cdyne.com/emailverify/emailvernotestemail.asmx"; soapmessage soapresponse = soapconnection.call(createsoaprequest(), url); // process soap response printsoapresponse(soapresponse); soapconnection.close(); } catch (exception e) { system.err.println("error occurred while sending soap request server"); e.printstacktrace(); } } private static soapmessage createsoaprequest() throws exception { messagefactory messagefactory = messagefactory.newinstance(); soapmessage soapmessage = messagefactory.createmessage(); soappart soappart = soapmessage.getsoappart(); string serveruri = "http://ws.cdyne.com/"; // soap envelope soapenvelope envelope = soappart.getenvelope(); envelope.addnamespacedeclaration("example", serveruri); /* constructed soap request message: <soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:example="http://ws.cdyne.com/"> <soap-env:header/> <soap-env:body> <example:verifyemail> <example:email>mutantninja@gmail.com</example:email> <example:licensekey>123</example:licensekey> </example:verifyemail> </soap-env:body> </soap-env:envelope> */ // soap body soapbody soapbody = envelope.getbody(); soapelement soapbodyelem = soapbody.addchildelement("verifyemail", "example"); soapelement soapbodyelem1 = soapbodyelem.addchildelement("email", "example"); soapbodyelem1.addtextnode("mutantninja@gmail.com"); soapelement soapbodyelem2 = soapbodyelem.addchildelement("licensekey", "example"); soapbodyelem2.addtextnode("123"); mimeheaders headers = soapmessage.getmimeheaders(); headers.addheader("soapaction", serveruri + "verifyemail"); soapmessage.savechanges(); /* print request message */ system.out.print("request soap message = "); soapmessage.writeto(system.out); system.out.println(); return soapmessage; } /** * method used print soap response */ private static void printsoapresponse(soapmessage soapresponse) throws exception { transformerfactory transformerfactory = transformerfactory.newinstance(); transformer transformer = transformerfactory.newtransformer(); source sourcecontent = soapresponse.getsoappart().getcontent(); system.out.print("\nresponse soap message = "); streamresult result = new streamresult(system.out); transformer.transform(sourcecontent, result); } }
(the code above taken , adapted this page.)
Comments
Post a Comment