【发布时间】:2014-04-04 17:02:09
【问题描述】:
我想在链接中调用以下网络服务:
http://www.webservicex.net/globalweather.asmx?op=GetCitiesByCountry
这是主程序:
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());
}
}
我收到以下错误响应:
<?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. --->
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>
有人有答案吗?
【问题讨论】:
-
您应该从 WSDL 开始 - "webservicex.net/globalweather.asmx?WSDL"
-
这是发送前的 xml 文件:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ansi:GetCitiesByCountry xmlns:ansi="http://www.webserviceX.NET"><CountryName>Morocco</CountryName></ansi:GetCitiesByCountry></SOAP-ENV:Body></SOAP-ENV:Envelope>
标签: java web-services soap