【问题标题】:A java-based soap request returns always soap response with 0 as the value i need基于java的soap请求总是返回以0作为我需要的值的soap响应
【发布时间】: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/"&gt; - 另外,更改所有内容。 XML 不是 HTML,XML 解析器非常迂腐。
  • 如果我理解正确的话,我只是在服务调用中添加了一个 QName 对象。它仍然是相同的结果。如果这不是你的建议的意思,我将不胜感激您发布了您对代码的建议修改。

标签: java soap webservice-client


【解决方案1】:

在我上面的例子中传递的正确字符串是:

String SOAP_REQUEST = 
"<soapenv:Envelope 
     xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"
     xmlns:web=\"http://www.webserviceX.NET/\">
 <soapenv:Header/>
 <soapenv:Body>
 <web:ConversionRate>
 <web:FromCurrency>EUR</web:FromCurrency>
 <web:ToCurrency>ILS</web:ToCurrency>
 </web:ConversionRate>
 </soapenv:Body></soapenv:Envelope>"

以防万一有人遇到此问题.. 我的声誉不允许我将其标记为正确。 非常感谢 mcfinnigan 帮助我..

【讨论】:

  • 如果有人对此有任何问题,请询问.. 我已经在这个问题上花了一些时间.. 我还构建了一个相应的 SOAPEnvelope 对象来表示这个或类似的请求..
【解决方案2】:

试试这个 XML

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

【讨论】:

  • 再次感谢您在这件事上与我保持联系。不幸的是,它确实产生了相同的输出.. ConversionRateResult 的值为 0.. 我怀疑它可能在我提供的 java 代码中,或者可能缺少什么?如果你能再看看真的很感激..这个错误让我很生气..
  • 不幸的是,我位于公司防火墙后面,因此无法编写代码并实际测试远程 Web 服务。你不能得到一份soapUI的副本,并在你访问它时用它来测试服务是否正常工作吗?
  • 我刚刚用soapUI试了一下,它工作了。我能看到的请求中唯一的区别是soapenv:Envelope而不是我的soap:Envelope。而且在soapUI中,还有web:ConversionRate而不是我的web:ConversionRate 那可能是什么?
  • 可能远程端的服务存根正在期待确切的 XML,soapUI 擅长于此。尝试在你的java程序中使用soapUI生成的xml。
  • 这个异常是我今天的一个朋友:org.xml.sax.SAXParseException: 元素“web:FromCurrency”的前缀“web”未绑定。每次我更改/尝试周围的东西时,另一个前缀都会出现这个异常......我想我会放弃这个并给它一个小时来尝试通过其适当的对象而不是字符串来构建请求..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-09
  • 2014-01-13
  • 1970-01-01
相关资源
最近更新 更多