vendredi 18 avril 2014

Appel d'un service web SOAP en java - Stack Overflow


I would like to calling the following web service:
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"));
// namespace to use for my rpc/encoded wsdl version is:
// http://phonedirlux.homeip.net/wsdl
// note, in this case the endpoint address is /rcx-ws-rpc/rcx

// 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 :


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




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>


I would like to calling the following web service:
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"));
// namespace to use for my rpc/encoded wsdl version is:
// http://phonedirlux.homeip.net/wsdl
// note, in this case the endpoint address is /rcx-ws-rpc/rcx

// 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 :


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



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>

0 commentaires:

Enregistrer un commentaire