SOAP 服务器无法处理请求
SOAP Server was unable to process request
我对 SOAP 有疑问 request.I 想解释一下我在做什么。
这是我的 SOAP 请求。
<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>
端点:http://www.webservicex.net/globalweather.asmx
WSDL link : http://www.webservicex.net/globalweather.asmx?WSDL
这也是我的代码。
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);
}}
结果,我得到了"Server was unable to process.Procedure or function 'getWeather' expects parameter '@CountryName', which was not supplied."
这是什么意思?为什么我要接受这个例外?
关于解决方案有什么建议吗?
您正在使用变量 serverUrl
作为 HTTP 服务器 URL 和 XML 命名空间名称。它们很接近但不完全相同。名称空间名称是 http://www.webserviceX.NET
但您的服务器 URL 是 http://www.webserviceX.NET/
(注意尾部斜杠)。 XML 命名空间的字符串必须与架构中的命名空间名称完全匹配。
建议您为命名空间创建一个单独的变量(或直接将其内联):
String serverURL = "http://www.webserviceX.NET/";
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("web", "http://www.webserviceX.NET");
...
通过此更改,您的代码对我有用。
我对 SOAP 有疑问 request.I 想解释一下我在做什么。 这是我的 SOAP 请求。
<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>
端点:http://www.webservicex.net/globalweather.asmx
WSDL link : http://www.webservicex.net/globalweather.asmx?WSDL
这也是我的代码。
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);
}}
结果,我得到了"Server was unable to process.Procedure or function 'getWeather' expects parameter '@CountryName', which was not supplied."
这是什么意思?为什么我要接受这个例外?
关于解决方案有什么建议吗?
您正在使用变量 serverUrl
作为 HTTP 服务器 URL 和 XML 命名空间名称。它们很接近但不完全相同。名称空间名称是 http://www.webserviceX.NET
但您的服务器 URL 是 http://www.webserviceX.NET/
(注意尾部斜杠)。 XML 命名空间的字符串必须与架构中的命名空间名称完全匹配。
建议您为命名空间创建一个单独的变量(或直接将其内联):
String serverURL = "http://www.webserviceX.NET/";
SOAPEnvelope envelope = soapPart.getEnvelope();
envelope.addNamespaceDeclaration("web", "http://www.webserviceX.NET");
...
通过此更改,您的代码对我有用。