SOAP Server was unable to process request - soap

I have a problem about SOAP request.I want to explain what am I doing.
This is my SOAP request.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET">
<soapenv:Header/>
<soapenv:Body>
<web:GetWeather>
<!--Optional:-->
<web:CityName>Istanbul</web:CityName>
<!--Optional:-->
<web:CountryName>Turkey</web:CountryName>
</web:GetWeather>
</soapenv:Body>
</soapenv:Envelope>
Endpoint : http://www.webservicex.net/globalweather.asmx
WSDL link : http://www.webservicex.net/globalweather.asmx?WSDL
Also this is my code.
public static void main(String args[]) {
try {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory
.newInstance();
SOAPConnection soapConnection = soapConnectionFactory
.createConnection();
// Send SOAP Message to SOAP Server
String url = "http://www.webservicex.net/globalweather.asmx?WSDL";
// SOAPMessage soapResponse =
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(),
url);
// Process the SOAP Response
printSOAPResponse(soapResponse);
soapConnection.close();
} catch (Exception e) {
System.err.println("Error occurred while sending SOAP Request to Server");
e.printStackTrace();
}
}
private static SOAPMessage createSOAPRequest() throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURL = "http://www.webserviceX.NET/";
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("web", serverURL);
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapElement = soapBody.addChildElement("GetWeather", "web");
SOAPElement soapElement1 = soapElement.addChildElement("CityName",
"web");
soapElement1.addTextNode("Istanbul");
SOAPElement soapElement2 = soapElement.addChildElement("CountryName",
"web");
soapElement2.addTextNode("Turkey");
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", serverURL + "GetWeather");
soapMessage.saveChanges();
return soapMessage;
}
/**
* Method used to print the 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);
}}
As a result, I got "Server was unable to process.Procedure or function 'getWeather' expects parameter '#CountryName', which was not supplied."
What does it mean ? Why am I taking this exception ?
Any suggestion about solution ?

You are using the variable serverUrl as both the HTTP server URL and as the XML namespace name. They are close but not exactly the same. The namespace name is http://www.webserviceX.NET but your server URL is http://www.webserviceX.NET/ (notice the trailing slash). The string for an XML namespace must be an exact match to the namespace name in the schema.
Recommend you create a separate variable for the namespace (or just inline it):
String serverURL = "http://www.webserviceX.NET/";
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("web", "http://www.webserviceX.NET");
...
With this change, your code works for me.

Related

Webservice sending its WSDL definition as its response

I am sending a SOAP request to a webservice but it is sending its WSDL definition back as its response.
What would lead to this?
Response:
<?xml version="1.0" encoding="UTF-8"?><wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://www.sample.com/test_request" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://www.sample.com/test_request" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<wsdl:types>
<xsd:schema e
Code:
import javax.xml.soap.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
public class Test {
/**
* Starting point for the 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 to SOAP Server
String url = "https://xxxxxxxxxxx.xxxxxxxxx.com/xxxxxxxx.do?WSDL&xxxxxxxxx=qualified";
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
// Process the SOAP Response
printSOAPResponse(soapResponse);
soapConnection.close();
} catch (Exception e) {
System.err.println("Error occurred while sending SOAP Request to Server");
e.printStackTrace();
}
}
private static SOAPMessage createSOAPRequest() throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
String serverURI = "http://www.xxxxxxxx.com/xxxxxx";
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("a", "http://www.xxxxxxw.com/xxxxxxxx");
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("test", "a");
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("testid", "a");
soapBodyElem1.addTextNode("xxxxxxxxx");
MimeHeaders headers = soapMessage.getMimeHeaders();
headers.addHeader("SOAPAction", "http://www.xxxxxx-xxxxx.com/xxxxxxx/xxxx");
String username = "123";
String password = "123";
String authorization = new sun.misc.BASE64Encoder().encode((username + ":" + password).getBytes());
System.out.println(authorization);
headers.addHeader("Authorization", "Basic " + authorization);
headers.addHeader("Proxy-Connection","Keep-Alive");
soapMessage.saveChanges();
/* Print the request message */
System.out.println("Request: ");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
/**
* Method used to print the 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);
}
}
What have caused this issue?
I am getting proper response from SOAP UI
You specified in the URL to get the WSDL (Parameter ?WSDL). You need to specify the proper URL for the service operation you want to call.

how to remove namespace prefix soap handler handleMessage

I need to remove all namespace prefix "bim" in the following soap request . How do i do that inside soap handler handleMessage?
<bim:AuthorizationInfo>
<!--Optional:-->
<bim:ApplicationId>123</bim:ApplicationId>
<!--Optional:-->
<bim:UserId>123</bim:UserId>
<!--Optional:-->
<bim:Userid>123</bim:Userid>
<bim:Password>123</bim:Password>
</bim:AuthorizationInfo>
This code is not working:
SOAPMessage soapMsg = context.getMessage();
SOAPEnvelope envelope = soapMsg.getSOAPPart().getEnvelope();
javax.xml.soap.SOAPBody body = envelope.getBody();
#SuppressWarnings("rawtypes")
Iterator iter = body.getChildElements();
while (iter.hasNext()) {
Object object = iter.next();
if (object instanceof SOAPElement) {
SOAPElement element = (SOAPElement) object;
element.removeNamespaceDeclaration(element.getPrefix());
element.setPrefix("");
}
}

SAAj SOAP CLIENT

I am using JDK 1.5 with SAAJ [saaj-api-1.3.jar and saaj-impl-1.3.15.jar] and activation.jar
Now I have simple client below: On running this I am just getting the response just as ERROR tag nothing else, its very confusion, I thought there is something wrong with the webservice, so I have printed the SOAP_MESSAGE generated by SAAJ and using SOAP-UI sent the exact same message and it gave me the correct response, I even tried another webservice from
URL [http://www.actionscript.org/forums/showthread.php3?t=70742] and it seem to be working correctly. Please someone let me know I am totally lost here. Thanks in advance.
import java.io.IOException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
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;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.OutputKeys;
public class Test {
// Method for creating the SOAP Request
private static SOAPMessage createSOAPRequest() throws Exception {
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage soapMessage = messageFactory.createMessage();
SOAPPart soapPart = soapMessage.getSOAPPart();
// Construct SOAP Request Message:
// SOAP Envelope
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("sch", "http://www.cpscreen.com/schemas");
// SOAP Body
SOAPBody soapBody = envelope.getBody();
SOAPElement soapBodyElem = soapBody.addChildElement("CPLinkRequest","sch");
QName attributeName1 = new QName("account");
soapBodyElem.addAttribute(attributeName1, "NOTEST");
QName attributeName2 = new QName("userId");
soapBodyElem.addAttribute(attributeName2, "NONAME");
QName attributeName3 = new QName("password");
soapBodyElem.addAttribute(attributeName3, "NOPASSWORD");
SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("Type", "sch");
soapBodyElem1.addTextNode("Report");
SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("ProviderReferenceId", "sch");
soapBodyElem2.addTextNode("WPS-6472130");
soapMessage.saveChanges();
// Check the input
System.out.println("Request SOAP Message for Product web service");
soapMessage.writeTo(System.out);
System.out.println();
return soapMessage;
}
// Method for receiving the SOAP Response
private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","2");
Source sourceContent = soapResponse.getSOAPPart().getContent();
System.out.println("\nResponse SOAP Message from Product web service : ");
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
}
// Starting point for SaajClient
public static void main(String args[]) {
try {
// Create SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Sending SOAP Message to SOAP Server i.e, Product Catalog service
//String url = "http://www.webservicex.net/convertFrequency.asmx?WSDL";
java.net.URL endpoint = new URL("https://abc.xyz.com/pub/aaa/ws/backgroundCheck");
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(),endpoint);
// Processing the SOAP Response
printSOAPResponse(soapResponse);
//System.out.print("Response SOAP Message:");
//soapResponse.writeTo(System.out);
soapConnection.close();
} catch (Exception e) {
System.err.println("Error occurred while sending SOAP Request to Server");
e.printStackTrace();
}
}
}

Cannot unmarshal soap body to java object

This is related to my earlier post here getSOAPBody returns NULL whereas SOAPResponse.writeTo prints the whole message, Strange?
I am posting my code that i am using to unmarshal. I receive all nulls in the target object
package trials;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
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;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.stream.StreamSource;
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);
SOAPBody body = soapResponse.getSOAPBody();
System.out.println("\n");
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());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
soapResponse.writeTo(bos);
XMLInputFactory xif = XMLInputFactory.newFactory();
StreamSource xml = new StreamSource(new ByteArrayInputStream(bos.toByteArray()));
XMLStreamReader xsr = xif.createXMLStreamReader(xml);
xsr.nextTag();
while (!xsr.getLocalName().equals("VerifyEmailResult")) {
xsr.nextTag();
}
JAXBContext jc = JAXBContext.newInstance(VerifyEmailResult.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<VerifyEmailResult> jb = unmarshaller.unmarshal(xsr, VerifyEmailResult.class);
xsr.close();
VerifyEmailResult v = jb.getValue();
System.out.println(v.ResponseText);
System.out.println(v.ResponseCode);
System.out.println(v.LastMailServer);
System.out.println(v.GoodEmail);
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;
}
}
Here is my class which i am trying to unmarshal to
package trials;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class VerifyEmailResult {
public String ResponseText;
public String ResponseCode;
public String LastMailServer;
public String GoodEmail;
}
Here is my console output
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>mutantninja#gmail.com</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>Current license key only allows so many checks</ResponseText><ResponseCode>9</ResponseCode><LastMailServer/><GoodEmail>false</GoodEmail></VerifyEmailResult></VerifyEmailResponse></soap:Body></soap:Envelope>
Current license key only allows so many checks
9
false
null
null
null
null
Try with
response.response.getSOAPBody().extractContentAsDocument()
I was facing the same problem and it is fixed with below code :
SOAPMessage response = dispatch.invoke(request);
Document document = response.getSOAPBody().extractContentAsDocument();
NodeList list = document.getChildNodes();
System.out.println("Value : " + list.item(0).getChildNodes().item(0).getChildNodes().item(0).getTextContent());
Hope this helps

format SOAP response

I am trying to create a web service to produce a simple soap response to a ping request:
<soap:Envelope>
<soap:Body>
<CustomRS attr="somevalue">
<Success/>
</CustomRS>
</soap:Body>
</soap:Envelope>
Instead, I get this response
<soap:Envelope>
<soap:Body>
<PingResponse>
<CustomRS attr="somevalue">
<Success/>
</CustomRS>
</PingResponse>
</soap:Body>
</soap:Envelope>
"Ping" is the name of my WebMethod and CustomRS is my Serializable response object. How do I get rid of the PingResponse element and just have the CustomRS as the root element?
My Implementation
#WebService (name = '', serviceName = ''targetNamespace = '')
#Stateless (mappedName = '')
public class TestEjb implements Testnterface {
#SOAPBinding(style=Style.DOCUMENT, use=Use.LITERAL, parameterStyle=ParameterStyle.BARE)
#WebResult (name = "CustomRS", targetNamespace = "name space")
#WebMethod (operationName = "CustomRS")
public CustomRS_OutPut Ping( #WebParam (name = "header",Type type,
#WebParam (name = "parameters", Param param) throws Exception
{
}
public SoapObject soap(String METHOD_NAME, String SOAP_ACTION,
String NAMESPACE, String URL) throws IOException,
XmlPullParserException {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); // set up
// request
request.addProperty("iTruckId", "1");
request.addProperty("iLocationId", "1");// variable name, value. I got
// the variable name, from the
// wsdl file!
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11); // put all required data into a soap
// envelope
envelope.setOutputSoapObject(request); // prepare request
AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);
httpTransport.debug = true; // this is optional, use it if you don't
// want to use a packet sniffer to check
// what the sent
// message was (httpTransport.requestDump)
httpTransport.call(SOAP_ACTION, envelope); // send request
SoapObject result = (SoapObject) envelope.getResponse(); // get response
return result;
}
}