I implemented a soap client to call a web service method of a third person. The method is: InsertData_Str
I've a problem with my java application. I need to add to the InsertData_Str method the xmlns attribute but it doesn't work, it put an empty value and I don't understand why. Any idea?
Here is the code:
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
soapMessage.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
soapMessage.setContentDescription("MY Connector");
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "http://www.ik.com/ikConnect";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.setPrefix("soap");
SOAPBody soapBody = envelope.getBody();
SOAPElement soapMethod = soapBody.addChildElement("InsertData_Str"); //Method
//soapMethod.setAttribute("xmlns", "http://www.ik.com/ikConnect"); //This doesn't work
QName attributeName = new QName("xmlns");
soapMethod.addAttribute(attributeName,"http://www.ik.com/ikConnect"); //If I Debugg I can see that xmln attribute is OK but when the message is sent xmln is empty
Here is the output:
<?xml version="1.0" encoding="utf-8" ?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><InsertData_Str xmlns=""><xdoc xmlns="http://www.ik.com/ikConnect">TEST</xdoc></InsertData_Str></SOAP-ENV:Body></soap:Envelope>
I solved it.
I changed SOAPElement addChildElement(String localName) method by this other one addChildElement(String localName,String prefix,String uri)
Example:
String serverURI = "http://www.ik.com/ikConnect";
SOAPElement soapMethod = soapBody.addChildElement("InsertData_Str", "", serverURI);
Related
I am new to SOAP. I want to create a soap request of given below xml.
xml
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<SendRequest xmlns="http://tempuri.org/">
<request xsi:type="RegisterCheckRequest" Id="7a646d45-ee2f-4b1c-8de8-780c416fbbd0" Service="42" xmlns="http://paygo24.com/v3/protocol">
<PaymentParameters xmlns="">
<Parameter Name="account" Value="08374829" />
</PaymentParameters>
</request>
<pointId>46</pointId>
<password>4QrcOUm6Wau+VuBX8g+IPg==</password>
</SendRequest>
</soap:Body>
</soap:Envelope>
I was using below sample java file, to create soap request but I am not able to do so, can anyone give me support.
Sample Java File to create soap request
import javax.xml.soap.*;
public class SOAPClientSAAJ {
public static void main(String args[]) throws Exception {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
String url = "http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx";
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
// print SOAP Response
System.out.print("Response SOAP Message:");
soapResponse.writeTo(System.out);
soapConnection.close();
}
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 the request message */
System.out.print("Request SOAP Message:");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
}
This is how you build soap request
private static SOAPMessage createSOAPRequest() throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.removeNamespaceDeclaration(envelope.getPrefix());
envelope.addNamespaceDeclaration("soap","http://schemas.xmlsoap.org/soap/envelope/");
envelope.setPrefix("soap");
envelope.addNamespaceDeclaration("xsi","http://www.w3.org/2001/XMLSchema-instance");
envelope.addNamespaceDeclaration("xsd","http://www.w3.org/2001/XMLSchema");
SOAPHeader header=soapMessage.getSOAPHeader();
header.setPrefix("soap");
SOAPBody soapBody = envelope.getBody();
soapBody.setPrefix("soap");
SOAPElement root=soapBody.addChildElement(new QName("http://tempuri.org/","SendRequest"));
SOAPElement request=root.addChildElement(new QName("http://paygo24.com/v3/protocol","request"));
request.setAttribute("xsi:type", "RegisterCheckRequest");
request.setAttribute("Id","7a646d45-ee2f-4b1c-8de8-780c416fbbd0");
request.setAttribute("Service","42");
SOAPElement paymentParameters =request.addChildElement(new QName(" ","PaymentParameters"));
SOAPElement parameter=paymentParameters.addChildElement("Parameter");
parameter.setAttribute("Name","account");
parameter.setAttribute("Value", "08374829");
root.addChildElement("pointId").setValue("46");
root.addChildElement("password").setValue("4QrcOUm6Wau+VuBX8g+IPg==");
soapMessage.saveChanges();
soapMessage.writeTo(System.out);
return soapMessage;
}
You can generate XML from objects using annotations to tie class members to XML elements/attributes. A popular way is using JAX-B. See this tutorial: http://www.vogella.com/tutorials/JAXB/article.html
the API says I have to use the method "getStock" and following parameters:
accessToken,company,itemNumber,commissionNumber.
I wrote this code but it doesn't work.
SOAPEnvelope envelope = soapPart.getEnvelope();
//envelope.addNamespaceDeclaration("sam", "http://samples.axis2.techdive.in");
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement method = soapBody.addChildElement("getStock");
SOAPElement firstParam = method.addChildElement("accessToken");
firstParam.addTextNode("xxx");
SOAPElement secondParam = method.addChildElement("company");
secondParam.addTextNode("AS");
SOAPElement thirdParam = method.addChildElement("itemNumber");
thirdParam.addTextNode("020001");
SOAPElement fourthParam = method.addChildElement("commissionNumber");
fourthParam.addTextNode("0");
soapMessage.saveChanges();
And what about the NamespaceDeclaration?
I get this error:
Response SOAP Message =
ns2:Client
Cannot find dispatch method for {}getStock
Process finished with exit code 0
Greetings Andrew
In order to specify the prefix and the name space, you can use a Qname object (import javax.xml.namespace.QName;) like this:
QName stockQname = new QName("http://your_namespace_uri.com",
"getStock", "prefix");
Change the prefix to your actual prefix.
Change the namespace to your actual namespaceuri
I rewrote your code to use Qnames:
private static void test() throws SOAPException {
MessageFactory factory = MessageFactory
.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
SOAPMessage message = factory.createMessage();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody soapBody = envelope.getBody();
QName stockQname = new QName("http://your_namespace_uri.com",
"getStock", "prefix");
SOAPBodyElement stockElement = soapBody.addBodyElement(stockQname);
QName accessQname = new QName("accessToken");
SOAPElement accessElement = stockElement.addChildElement(accessQname);
accessElement.addTextNode("xxx");
QName companyQname = new QName("company");
SOAPElement companyElement = stockElement.addChildElement(companyQname);
companyElement.addTextNode("AS");
QName itemQname = new QName("itemNumber");
SOAPElement itemElement = stockElement.addChildElement(itemQname);
itemElement.addTextNode("020001");
QName commisionQname = new QName("commissionNumber");
SOAPElement commissionElement = stockElement
.addChildElement(commisionQname);
commissionElement.addTextNode("0");
message.saveChanges();
}
And this is the generated SOAP message:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<prefix:getStock xmlns:prefix="http://your_namespace_uri.com">
<accessToken>xxx</accessToken>
<company>AS</company>
<itemNumber>020001</itemNumber>
<commissionNumber>0</commissionNumber>
</prefix:getStock>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
I am using this SAAJ library
<dependency>
<groupId>com.sun.xml.messaging.saaj</groupId>
<artifactId>saaj-impl</artifactId>
<version>1.3.25</version>
</dependency>
If you want to use SOAP 1.2 message protocol, simply change this line:
MessageFactory factory = MessageFactory
.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
To this line:
MessageFactory factory = MessageFactory
.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
Hope this helps
Sample Webservice Mehtod
public String getMsg(String arg1,String arg2)
{
System.out.println("arg1--->"+arg1);
System.out.println("arg2--->"+arg2);
return "response";
}
Client Code
private static SOAPMessage createSOAPRequest() throws Exception
{
System.out.println("createSOAPRequest---->");
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "http://webservice.jaipal.econnectsolution.com";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("MineralWebService", serverURI);
//SOAP Body
SOAPBody soapBody = envelope.getBody();
System.out.println("soapBody----->"+soapBody);
SOAPElement soapBodyElem = soapBody.addChildElement("getMsg", "MineralWebService",serverURI);
SOAPElement value = soapBodyElem.addChildElement("getMsg","MineralWebService");
value.setTextContent("Arguments One");
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURI + "add");
System.out.println("headers----->"+headers.toString());
soapMessage.saveChanges();
/* Print the request message */
System.out.print("Request SOAP Message = ");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
I want To add wwo arguments to call my webservice mehtod. Using above code, I was Able to send only one Argument.
Please help me to achieve this.
Have you tried creating/adding new element, like you are doing it for first argument?
SOAPElement soapBodyElem2 = soapBody.addChildElement("getMsg", "MineralWebService",serverURI);
SOAPElement value2 = soapBodyElem2.addChildElement("getMsg","MineralWebService");
value2.setTextContent("Arguments Two");
I am not familiar with SOAP requests so I have been reading up on it. The examples and tutorials I've looked at construct the request in an xml format in the SOAP envelope.
example:
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 the request message */
System.out.print("Request SOAP Message = ");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
but the example request that was given to me by the organization with the server is a single line with the request in the URL:
http://sdmdataaccess.nrcs.usda.gov/Spatial/SDMNAD83Geographic.wfs?Service=WFS&Version=1.0.0&Request=GetFeature&OutputFormat=GML3&TypeName=MapunitPoly&FILTER=<Filter><Intersect><PropertyName>Geometry</PropertyName><gml:Polygon><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>-95.1852448111,43.0186163988 -95.1853350008,43.0183961223 -95.1854898978,43.0183055981 -95.1858276893,43.0182603358 -95.1861146851,43.0183087828 -95.1862558373,43.0184050072 -95.186496397,43.0188380162 -95.1867287441,43.018969948 -95.1871860608,43.0190950058 -95.1872413814,43.0192924831 -95.1869109659,43.0195048805 -95.1863026073,43.019660449 -95.1860721056,43.0196581015 -95.185922908,43.0195072276 -95.1857936581,43.0191228335 -95.1853686954,43.0188252761 -95.1852448111,43.0186163988 -95.1852448111,43.0186163988</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></Intersect></Filter>
Here it is again but wrapped and indented for easier reading:
http://sdmdataaccess.nrcs.usda.gov/Spatial/SDMNAD83Geographic.wfs?
Service=WFS&
Version=1.0.0&
Request=GetFeature&
OutputFormat=GML3&
TypeName=MapunitPoly&
FILTER=<Filter>
<Intersect>
<PropertyName>Geometry</PropertyName>
<gml:Polygon>
<gml:outerBoundaryIs>
<gml:LinearRing>
<gml:coordinates>
-95.1852448111,43.0186163988
-95.1853350008,43.0183961223
-95.1854898978,43.0183055981
-95.1858276893,43.0182603358
-95.1861146851,43.0183087828
-95.1862558373,43.0184050072
-95.186496397,43.0188380162
-95.1867287441,43.018969948
-95.1871860608,43.0190950058
-95.1872413814,43.0192924831
-95.1869109659,43.0195048805
-95.1863026073,43.019660449
-95.1860721056,43.0196581015
-95.185922908,43.0195072276
-95.1857936581,43.0191228335
-95.1853686954,43.0188252761
-95.1852448111,43.0186163988
-95.1852448111,43.0186163988
</gml:coordinates>
</gml:LinearRing>
</gml:outerBoundaryIs>
</gml:Polygon>
</Intersect>
</Filter>
I'm not sure how to either modify this to handle the single line request or to modify the request to fit into this system. Any information would be helpful.
That's not SOAP. That's a simple GET request, in which one parameter is an XML document. As such, you can simply issue a GET request using Apache Http Components or similar.
Note that if you take the above URL and request it via your browser (a GET) you'll get an XML response back (which you'll need to parse appropriately)
I am able to generate a SOAP message but I do not know
add prefix only to soapMessage tag (should not have namespace)
SOAPConnectionFactory soapConnectionFactory =
SOAPConnectionFactory.newInstance();
SOAPConnection connection =
soapConnectionFactory.createConnection();
SOAPFactory soapFactory =
SOAPFactory.newInstance();
MessageFactory factory =
MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SOAPMessage message = factory.createMessage();
SOAPHeader header = message.getSOAPHeader();
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
SOAPBody body = soapEnvelope.getBody();
soapEnvelope.removeNamespaceDeclaration(soapEnvelope.getPrefix());
soapEnvelope.setPrefix("soap");
body.setPrefix("soap");
header.removeNamespaceDeclaration(header.getPrefix());
header.setPrefix("soap");
soapEnvelope.addNamespaceDeclaration("v9", "URL TO SERVER");
Name bodyName;
bodyName = soapFactory.createName("SearchHotels");
SOAPBodyElement getList = body.addBodyElement(bodyName);
getList.setPrefix("v9");
Name childName = soapFactory.createName("SoapMessage", "v9", "URL TO SERVER");
SOAPElement HotelListRequest = getList.addChildElement(childName);
HotelListRequest.addChildElement("Hotel", "v9").addTextNode("Hilton");
My SOAP Message
...
<v9:SoapMessage xmlns:els="URL TO SERVER">
...
What I expect
...
<v9:SoapMessage>
...
Update :
If I use the following it runs into following error
SOAPElement HotelListRequest = getList.addChildElement("v9:SoapMessage");
org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create or change an object in a
way which is incorrect with regard to namespaces.
To add the namespace prefix to all the tags you have to redeclare the desired prefix (and eventually the namespace) on every inserted child, otherwise it will inherit the namespace (implicitely) from the parent element.
Try for instance:
SOAPBodyElement getList = body.addBodyElement(bodyName, "v9", "http://URL TO SERVER");
or
soapBody.addChildElement("SomeElement", "v9", "http://URL TO SERVER");
or
soapBody.addChildElement("v9:SomeElement");
Sometimes you may have to use a QName object instead of just a String or a Name.
It pretty much depends on the SOAP-API/Implementation you use, but the principle is the same everywhere: either redeclare (explicit) or inherit (implicit).
In your expected SOAP message there is a difference in case of prefix , seems you are writing client where requesting to a service provider which is developed in .net , this is why you may need to change prefix .
I think you can take concept from bellow code :
MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
SOAPBody soapBody = soapEnvelope.getBody();
soapEnvelope.removeNamespaceDeclaration(soapEnvelope.getPrefix());
soapEnvelope.addNamespaceDeclaration("soap", "http://schemas.xmlsoap.org/soap/envelope/");
soapEnvelope.setPrefix("soap");
soapBody.setPrefix("soap");
soapEnvelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
soapEnvelope.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema"); soapMessage.getSOAPHeader().detachNode();
soapMessage.getMimeHeaders().setHeader("SOAPAction", "http://www.example.com/TransactionProcess");
For more detail please visit this REFERENCE LINK