MrArbi
MrArbi

Reputation: 25

Calling a web service SOAP in java

I would like to calling the following web service in the link :
http://www.webservicex.net/globalweather.asmx?op=GetCitiesByCountry

This is the main program:

public static void main(String[] args) {
    try {
        // Create the connection
        SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
        SOAPConnection conn = scf.createConnection();

        // Create message
        MessageFactory mf = MessageFactory.newInstance();
        SOAPMessage msg = mf.createMessage();

        // Add eventually a SoapAction header if necessary

        MimeHeaders hd = msg.getMimeHeaders(); hd.addHeader("SOAPAction", "http://www.webserviceX.NET/GetCitiesByCountry");

        // Object for message parts
        SOAPPart sp = msg.getSOAPPart();

        SOAPEnvelope env = sp.getEnvelope();

        SOAPBody bd = env.getBody();

        // Populate body
        // Main element and namespace
        SOAPElement be = bd.addChildElement(env.createName("GetCitiesByCountry", "ansi", "http://www.webserviceX.NET"));

        // Add content
        be.addChildElement("CountryName").addTextNode("Morocco");

        // Save message
        msg.saveChanges();

        // View input
        System.out.println("\n Soap request:\n");
        msg.writeTo(System.out);
        System.out.println();

        // Send
        String urlval = "http://www.webservicex.net/globalweather.asmx";
        // or /rcx-ws-rpc/rcx for my rpc/encoded web service

        SOAPMessage rp = conn.call(msg, urlval);

        // View the output
        System.out.println("\nXML response\n");

        // Create transformer
        TransformerFactory tff = TransformerFactory.newInstance();
        Transformer tf = tff.newTransformer();

        // Get reply content
        Source sc = rp.getSOAPPart().getContent();

        // Set output transformation
        StreamResult result = new StreamResult(System.out);
        tf.transform(sc, result);
        System.out.println();

        // Close connection
        conn.close();

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

I got the following error in response :

<?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>

Someone have an answer ?

Upvotes: 1

Views: 1634

Answers (2)

MrArbi
MrArbi

Reputation: 25

I m removing the context "ansi" and its work

    // Main element and namespace
    SOAPElement be = bd.addChildElement(env.createName("GetCitiesByCountry", "", "http://www.webserviceX.NET"));

thank you all

Upvotes: 0

nkuebelbeck
nkuebelbeck

Reputation: 283

I always use SOAPUI to test my web service call before wrapping a program around it. http://www.soapui.org/

In your case the wsdl is here http://www.webservicex.net/globalweather.asmx?WSDL

make sure your xml soap works there...

whats your xml file before it's sent?

Should be something like this

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

Upvotes: 3

Related Questions