2015-08-27 20 views
1

我正嘗試連接到this SOAP API。具體而言,我試圖通過this login call登錄。如何使用Savon進行包含命名空間的SOAP調用

文檔狀態我應進行如下請求:

POST /service/replicatorV4.asmx HTTP/1.1 
Host: demo12231.srv106.webshopdemo.net 
Content-Type: text/xml; charset=utf-8 
Content-Length: length 
SOAPAction: "http://tempuri.org/Login" 

<?xml version="1.0" encoding="utf-8"?> 
<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> 
    <Login xmlns="http://tempuri.org/"> 
     <username>string</username> 
     <password>string</password> 
    </Login> 
    </soap:Body> 
</soap:Envelope> 

當我在例如了SoapUI提出了以下要求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"> 
    <soapenv:Header/> 
    <soapenv:Body> 
    <tem:Login> 
     <tem:username>Administrator</tem:username> 
     <tem:password>passw0rd12</tem:password> 
    </tem:Login> 
    </soapenv:Body> 
</soapenv:Envelope> 

然後我得到這個滿意的迴應:

​​

然而,當我與薩翁作出這樣的請求:

require 'savon' 

client   = Savon.client(:wsdl => "http://demo12231.srv106.webshopdemo.net/service/replicatorV4.asmx?WSDL", 
           :open_timeout => 100, 
           :read_timeout => 100, 
           :ssl_verify_mode => :none, 
           :log_level => :debug, 
           :log => false, 
           :logger => Rails.logger, 
           :convert_request_keys_to => :camelcase) 

response  = client.call(:login, :message => {:username => "Administrator", :password => "passw0rd12"}) 

那麼我得到這個不滿意的迴應:

#<Savon::Response:0x007ff78cfcc368 
@globals= 
    #<Savon::GlobalOptions:0x007ff782fb8160 
    @option_type=:global, 
    @options= 
    {:encoding=>"UTF-8", 
    :soap_version=>1, 
    :namespaces=>{}, 
    :logger=> 
     #<ActiveSupport::Logger:0x007ff78b136ea8 
     @default_formatter= 
     #<Logger::Formatter:0x007ff78b136e30 @datetime_format=nil>, 
     @formatter= 
     #<ActiveSupport::Logger::SimpleFormatter:0x007ff783e99f80 
     @datetime_format=nil>, 
     @level=0, 
     @logdev= 
     #<Logger::LogDevice:0x007ff78b136de0 
     @dev= 
      #<File:/Users/me/Projects/my_project/log/development.log>, 
     @filename=nil, 
     @mutex= 
      #<Logger::LogDevice::LogDeviceMutex:0x007ff78b136db8 
      @mon_count=0, 
      @mon_mutex=#<Mutex:0x007ff78b136d68>, 
      @mon_owner=nil>, 
     @shift_age=nil, 
     @shift_size=nil>, 
     @progname=nil>, 
    :log=>false, 
    :filters=>[], 
    :pretty_print_xml=>false, 
    :raise_errors=>true, 
    :strip_namespaces=>true, 
    :convert_response_tags_to=> 
     #<Proc:[email protected]/Users/me/.rvm/gems/ruby-2.0.0-p247/gems/savon-2.11.1/lib/savon/options.rb:85 (lambda)>, 
    :convert_attributes_to=> 
     #<Proc:[email protected]/Users/me/.rvm/gems/ruby-2.0.0-p247/gems/savon-2.11.1/lib/savon/options.rb:86 (lambda)>, 
    :multipart=>false, 
    :adapter=>nil, 
    :use_wsa_headers=>false, 
    :no_message_tag=>false, 
    :follow_redirects=>false, 
    :unwrap=>false, 
    :host=>nil, 
    :wsdl=> 
     "http://demo12231.srv106.webshopdemo.net/service/replicatorV4.asmx?WSDL", 
    :open_timeout=>100, 
    :read_timeout=>100, 
    :ssl_verify_mode=>:none, 
    :convert_request_keys_to=>:camelcase, 
    :endpoint=> 
     #<URI::HTTP:0x007ff78479cbf0 URL:http://demo12231.srv106.webshopdemo.net/service/replicatorV4.asmx>}>, 
@http= 
    #<HTTPI::Response:0x007ff78cfcd2b8 
    @body= 
    "<?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><LoginResponse xmlns=\"http://tempuri.org/\"><LoginResult xsi:nil=\"true\" /></LoginResponse></soap:Body></soap:Envelope>", 
    @code=200, 
    @headers= 
    {"Cache-Control"=>"private, max-age=0", 
    "Content-Type"=>"text/xml; charset=utf-8", 
    "Server"=>"Microsoft-IIS/8.5", 
    "Date"=>"Thu, 27 Aug 2015 05:29:42 GMT", 
    "Content-Length"=>"335"}, 
    @raw_body= 
    "<?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><LoginResponse xmlns=\"http://tempuri.org/\"><LoginResult xsi:nil=\"true\" /></LoginResponse></soap:Body></soap:Envelope>">, 
@locals= 
    #<Savon::LocalOptions:0x007ff78ce7f550 
    @option_type=:local, 
    @options= 
    {:advanced_typecasting=>true, 
    :response_parser=>:nokogiri, 
    :multipart=>false, 
    :message=>{:username=>"Administrator", :password=>"passw0rd12"}, 
    :soap_action=>"http://tempuri.org/Login"}>> 

響應的主體部分是:

{:login_response=>{:login_result=>nil, :@xmlns=>"http://tempuri.org/"}} 

,這告訴我,也許該服務確實收到了總體要求,但沒有得到的變量。響應看起來很像的響應,當我在了SoapUI使用錯usernamepassword這個請求:

<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> 
    <LoginResponse xmlns="http://tempuri.org/"> 
     <LoginResult xsi:nil="true"/> 
    </LoginResponse> 
    </soap:Body> 
</soap:Envelope> 

我注意到,命名空間tem變量的預期。這可能與我的問題有關嗎?還是有另一種解釋?

+0

你打開了登錄你的客戶端,並將輸出與你的SoapUI消息進行比較嗎? –

回答

2

創建一個沒有Rails的純Ruby腳本。

然後根據您使用SoapUI創建的內容檢查輸出。張貼差異,有人將能夠提供幫助。

當我運行此腳本:

require 'savon' 

c = Savon.client(wsdl: 
       "http://demo12231.srv106.webshopdemo.net/service/replicatorV4.asmx?WSDL", 
       log_level: :debug, 
       log: true, 
       pretty_print_xml: true) 
response = c.call(:login, 
        :message => {:username => "Administrator", :password => "passw0rd12"}) 

它爲我工作。這可能是你的Rails部分中的一些干擾。我不做Rails,因此我在這裏沒什麼幫助(我更喜歡Sinatra :-))。

+0

嗯,我不知道爲什麼這些設置使它工作,但他們沒有...謝謝。 – ChristofferJoergensen

+0

我可以看到的唯一主要區別是在代碼中使用了:convert_request_keys。如果你刪除了這個參數,事情可能會發揮作用。 –

相關問題