Search code examples
xmlweb-servicessoapwsdl

JAVA SOAP server fault


I get this error when sending SOAP request. Please help me out what i am doing wrong.

Sending SOAP request:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><GetCitiesByCountry xmlns="http://www.webserviceX.NET/"><CountryName>India</CountryName></GetCitiesByCountry></SOAP-ENV:Body></SOAP-ENV:Envelope>
Received SOAP reply:
<?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><soap:Fault><faultcode>soap:Server</faultcode><faultstring>System.Web.Services.Protocols.SoapException: Server was unable to process request. ---&gt; System.Data.SqlClient.SqlException: Procedure or function 'getWCity' expects parameter '@CountryName', which was not supplied.
   at WebServicex.GlobalWeather.GetCitiesByCountry(String CountryName)
   --- End of inner exception stack trace ---</faultstring><detail /></soap:Fault></soap:Body></soap:Envelope>

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package clientwithoutwsdl;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPMessage;

public class ClientWithoutWSDL {

    private final static String SERVICE_HOST = "http://www.webserviceX.NET/";
    private final static String SERVICE_METHOD = "GetCitiesByCountry";
    private final static String SERVICE_ENDPOINT = "globalweather.asmx";

    private final static String country = "India";

    public static void main(String args[]) {
     try {
         // Create a new SOAP connection
         SOAPConnectionFactory soapConnectionFactory =
         SOAPConnectionFactory.newInstance();
         SOAPConnection soapConnection = soapConnectionFactory.createConnection();

         // Send a SOAP Message to SOAP server
         // Send this message to http://www.webserviceX.NET/stockquote.asmx
         SOAPMessage soapResponse = soapConnection.call(
            createSOAPRequest(country),
            SERVICE_HOST + SERVICE_ENDPOINT);

         System.out.println("Received SOAP reply:");
         soapResponse.writeTo(System.out);
         System.out.println("\r\n");

         // Close the connection
         soapConnection.close();
      } catch (Exception e) {
         System.err.println("Fatal error occurred");
         e.printStackTrace();
      }
   }

    private static SOAPMessage createSOAPRequest(String stockSymbol) throws Exception {
        // Construct a new SOAP request message
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage soapMessage = messageFactory.createMessage();

        // Construct the SOAP "body" with the method arguments
        SOAPBody soapBody = soapMessage.getSOAPBody();
        QName bodyName = new QName(SERVICE_HOST, SERVICE_METHOD);
        SOAPBodyElement bodyElement = soapBody.addBodyElement(bodyName);

        SOAPElement soapBodyArgument1 = bodyElement.addChildElement("CountryName");
        soapBodyArgument1.addTextNode(country); 

        // Add a SOAP action header to the request

        MimeHeaders headers = soapMessage.getMimeHeaders();
        headers.addHeader("SOAPAction", SERVICE_HOST + SERVICE_METHOD);

        soapMessage.saveChanges();

        // Print out the request message:
        System.out.println("Sending SOAP request:");
        soapMessage.writeTo(System.out);
        System.out.printf("%n");
        return soapMessage;
    }
}

Solution

  • You XML printed

    <env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
        <env:Header/>
        <env:Body>
            <GetCitiesByCountry xmlns="http://www.webserviceX.NET/">
                <CountryName xmlns="">India</CountryName>
            </GetCitiesByCountry>
        </env:Body>
    </env:Envelope>
    

    Has a default namespace for countryName but should be http://www.webserviceX.NET.

    http://www.webservicex.net/globalweather.asmx?op=GetCitiesByCountry