Reputation: 41
I am providing a java code which i found on here somewhere on stack overflow and i want to use something similar for my purpose. This code works alright but there is a persistent issue that i face. Check the sys outs i have done in the code. The first 2 sys outs that i print to get the SOAP body of the response return me a null whereas when i try to print the whole response it prints accurately. Its very strange.
Does anyone know why this happens and how to fix it. I am using JDK 1.7
package trials;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
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);
System.out.println("Body");
// print SOAP Response
System.out.print("Response SOAP Message:");
System.out.println("SOAP Body 2= " + soapResponse.getSOAPBody());
System.out.println("SOAP Body 2=" + soapResponse.getSOAPPart().getEnvelope().getBody());
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>[email protected]</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("[email protected]");
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 the output that i get on console
Request SOAP 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>[email protected]</example:email><example:LicenseKey>123</example:LicenseKey></example:VerifyEmail></SOAP-ENV:Body></SOAP-ENV:Envelope>
Body
Response SOAP Message:SOAP Body 2= [soap:Body: null]
SOAP Body 2=[soap:Body: null]
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><VerifyEmailResponse xmlns="http://ws.cdyne.com/"><VerifyEmailResult><ResponseText>Mail Server will accept email</ResponseText><ResponseCode>3</ResponseCode><LastMailServer>gmail-smtp-in.l.google.com</LastMailServer><GoodEmail>true</GoodEmail></VerifyEmailResult></VerifyEmailResponse></soap:Body></soap:Envelope>
Upvotes: 4
Views: 10399
Reputation: 61
i followed the same tutorial as you did.
and faced the same problem, just if anybody else need this , i solved the problem by deleting the name space alias "example", in the inner nodes, I only left it in the first parameter node search
:
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("search", "example"); /*"example"*/
SOAPElement soapBodyElem0 = soapBodyElem.addChildElement("arg0");
SOAPElement soapBodyElem1 = soapBodyElem0.addChildElement("name");
soapBodyElem1.addTextNode("test");
SOAPElement soapBodyElem2 = soapBodyElem0.addChildElement("number");
soapBodyElem2.addTextNode("5");
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURI + "search");
as i saw in the SoapUI autogenerated XML (only in search
node)
<example:search>
<!--Optional:-->
<arg0>
<!--Optional:-->
<name>234</name>
<number>5</number>
</arg0>
</example:search>
This solved my problem.
Upvotes: 2
Reputation: 2240
toString()
for Node
is implemented as ["+getNodeName()+": "+getNodeValue()+"]
, and getNodeValue()
will be null for element nodes. Therefore, you'll get [soap:Body: null]
.
However, the body is there, and you can work with it as you normally would.
Try the following to see that all of the elements are there:
SOAPBody body = soapResponse.getSOAPBody();
System.out.println(body.getElementsByTagName("ResponseText").item(0).getTextContent());
System.out.println(body.getElementsByTagName("ResponseCode").item(0).getTextContent());
System.out.println(body.getElementsByTagName("GoodEmail").item(0).getTextContent());
Upvotes: 3