简单的Savon SOAP请求无法正常工作

时间:2011-05-11 15:46:34

标签: ruby savon

我有以下从soapUI生成的WSDL。当我测试 来自soapUI的请求,它工作正常,但从savon抛出异常。 我在Linux上使用savon 0.9.2。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/
envelope/" xmlns:mes="http://www.domain.com/sub/05/00/Messages">
   <soapenv:Header/>
   <soapenv:Body>
      <mes:loginRequest>
         <!--Optional:-->
         <mes:Username>admin</mes:Username>
         <!--Optional:-->
         <mes:Password>pass</mes:Password>
         <!--Optional:-->
         <mes:ImpersonationUsername></mes:ImpersonationUsername>
         <!--Optional:-->
         <mes:ApplicationName></mes:ApplicationName>
      </mes:loginRequest>
   </soapenv:Body>
</soapenv:Envelope>

导致错误的脚本:

require 'rubygems'
require 'savon'
require 'pp'

client = Savon::Client.new "http://domain/Service.asmx?WSDL"

response = client.request(:mes, "login") do
  soap.body = {
"mes:Username" => "test",
"mes:Password" => "test",
"mes:ImpersonationUsername"=>"Test",
"mes:ApplicationName"=>"test"
}
end
pp response.to_hash

输出:

D, [2011-05-10T16:06:14.316827 #11254] DEBUG -- : <?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><LoginResult xmlns="http://www.domain.com/sub/
05/00/
Messages"><ErrorDetails><Items><SfExceptionItem><ExceptionType>System.NullReferenceException</
ExceptionType><AssemblyName>mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b73a5c561934e089</
AssemblyName><Message>Object reference not set to an instance of an
object.</Message></SfExceptionItem></Items><FaultCode>UnknownError</
FaultCode><Message>Object reference not set to an instance of an
object.</Message></ErrorDetails></LoginResult></soap:Body></
soap:Envelope>
{:login_result=>
  {:error_details=>
    {:items=>
      {:sf_exception_item=>
        {:message=>"Object reference not set to an instance of an
object.",
         :exception_type=>"System.NullReferenceException",
         :assembly_name=>
          "mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b73a5c561934e089"}},
     :message=>"Object reference not set to an instance of an
object.",
     :fault_code=>"UnknownError"},
   :xmlns=>
    "http://www.domain.com/sub/05/00/Messages"}} 

SoapUI的输出如下:

<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>
      <LoginResult xmlns="http://domain/sub/05/00/Messages">
         <UserTicket>95bfaa81149f4c118c8724b837235cd5</UserTicket>
      </LoginResult>
   </soap:Body>
</soap:Envelope>

2 个答案:

答案 0 :(得分:0)

您可能希望在savon请求中设置命名空间:client.namespaces["xmlns:mes"] = "http://www.domain.com/sub/05/00/Messages"

答案 1 :(得分:0)

您发送的请求会创建以下肥皂消息

  <?xml version="1.0" encoding="UTF-8"?>
    <env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns:mes="urn:namespace.com"
                  xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
  <env:Body>
    <mes:login>
    <mes:Username>test</mes:Username>
    <mes:Password>test</mes:Password>
    <mes:ImpersonationUsername>Test</mes:ImpersonationUsername>
    <mes:ApplicationName>test</mes:ApplicationName>
    </mes:login>
  </env:Body>

我猜那不是你的意图吗? 请在.new

之后在代码中插入以下行
Savon.configure do |c|
  c.log = true
end

显示传出消息。