【发布时间】:2012-02-27 19:27:12
【问题描述】:
我正在尝试向一个开放的网络服务(webserviceX.NET 货币转换器)发送一个肥皂请求。这是我的代码:
String SOAP_REQUEST = "<SOAP:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP:Body><ConversionRate xmlns=\"http://www.webserviceX.NET/\"/>" +
"<FromCurrency>EUR</FromCurrency><ToCurrency>ILS</ToCurrency></SOAP:Body></SOAP:Envelope>";
// SOAPEnvelope env = new SOAPEnvelope();
//Create a Stream Source of the Request String
byte[] reqBytes = SOAP_REQUEST.getBytes();
ByteArrayInputStream bis = new ByteArrayInputStream(reqBytes);
StreamSource ss = new StreamSource(bis);
//Create a SOAP Message Object
MessageFactoryImpl messageFactory = new MessageFactoryImpl();
SOAPMessage msg = messageFactory.createMessage();
SOAPPart soapPart = msg.getSOAPPart();
//Set the soapPart Content with the stream source
soapPart.setContent(ss);
//Create a WebService Call
Service service = new Service();
Call call = (Call)service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endpoint));
call.setProperty( Call.SOAPACTION_USE_PROPERTY, new Boolean( true ) );
call.setProperty( Call.SOAPACTION_URI_PROPERTY, "http://www.webserviceX.NET/ConversionRate");
call.setEncodingStyle("utf-8");
//Invoke the WebService.
SOAPEnvelope resp = call.invoke(((org.apache.axis.SOAPPart)soapPart).getAsSOAPEnvelope());
...
//then i parse the resulting SOAPEnvelope get the value.
在此 url 中,您可以在与此 web 服务交互时看到 soap 请求和响应的性质。 http://www.webservicex.net/CurrencyConvertor.asmx?op=ConversionRate
我上面的 java 代码,获得在我提供和预期的 URL 中指定的正确和准确的肥皂响应。但总是将我需要的值设为 0。 这是我得到的肥皂响应:
<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><ConversionRateResponse
xmlns="http://www.webserviceX.NET/">
<ConversionRateResult>0</ConversionRateResult></ConversionRateResponse>
</soap:Body></soap:Envelope>
您可以看到 CoversionRateResult 为 0,尽管它应该返回 4.69。我尝试了他们提供的另一个网络服务,但我得到了这样的结果。
有人可以帮我看看我的 java 代码中可能缺少什么吗?我尝试了不同的变化,并且从一天开始就一直在玩:( .. 什么都没有。 我非常感谢任何人在这方面帮助我。 谢谢,
【问题讨论】:
-
您的问题可能与使用
SOAP元素而不是使用soap有关。 -
感谢您的回复。如果您的意思是代码开头的字符串中的那个(soap 请求),我刚试了一下,我得到了以下错误:org.xml.sax.SAXParseException: The prefix "soap" for element "soap:信封”未装订。
-
您还记得更改soap 命名空间的xmlns 吗?您上面的代码将其指定为
xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">- 另外,更改所有内容。 XML 不是 HTML,XML 解析器非常迂腐。 -
如果我理解正确的话,我只是在服务调用中添加了一个 QName 对象。它仍然是相同的结果。如果这不是你的建议的意思,我将不胜感激您发布了您对代码的建议修改。
标签: java soap webservice-client