2017-07-07 75 views
5

在使用Savon.rb的rails項目中,我試圖做出非常複雜的SOAP調用。至少複雜的薩翁建設者很麻煩,所以我決定直接操縱:xml。使用Savon的Ruby on Rails中的SOAP調用在信封和主要操作中變得很奇怪

的我第一次啓動客戶端:

@client = Savon.client(
      :endpoint => 'https://testservice.postnl.com/CIF_SB/BarcodeWebService/1_1/BarcodeWebService.svc', 
      :wsdl  => 'https://testservice.postnl.com/CIF_SB/BarcodeWebService/1_1/?wsdl') 

然後我打這個電話的形狀:

@request = @client.build_request(:generate_barcode, 
      xml: %Q{ ... see soap call (with ruby interpolation) ... } 

我添加了一個正確格式化l Time.now, format: :postnl_api串,其餘仍硬編碼。包括消息號碼。

以下是薩翁在這種情況下實際進行的調用的方式,使用@request.body進行檢索。

SOAP調用我的應用程序

<?xml version="1.0" encoding="UTF-8"?> 
<s:Envelope 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:wsdl="http://tempuri.org/" 
    xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Header> 
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://postnl.nl/cif/services/BarcodeWebService/IBarcodeWebService/GenerateBarcode 
    </Action> 
    <Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> 
     <UsernameToken> 
     <Username>devc_!R4xc8p9</Username> 
     <Password>xxxxxxxx</Password> 
     </UsernameToken> 
    </Security> 
    </s:Header> 
    <s:Body> 
    <wsdl:GenerateBarcode> 
     <d6p1:Customer> 
     <d6p1:CustomerCode>DEVC</d6p1:CustomerCode> 
     <d6p1:CustomerNumber>11223344</d6p1:CustomerNumber> 
     </d6p1:Customer> 
     <d6p1:Barcode> 
     <d6p1:Type>3S</d6p1:Type> 
     <d6p1:Range>DEVC</d6p1:Range> 
     <d6p1:Serie>1000000-2000000</d6p1:Serie> 
     </d6p1:Barcode> 
    </wsdl:GenerateBarcode> 
    </s:Body> 
</s:Envelope> 

再下面是一個電話應該是什麼樣子,因爲我已經看到了這個有一個在公司的沙箱環境中取得成功。

SOAP調用,因爲它應該是

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Header> 
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://postnl.nl/cif/services/BarcodeWebService/IBarcodeWebService/GenerateBarcode</Action> 
    <Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> 
     <wsse:UsernameToken xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> 
     <wsse:Username>devc_!R4xc8p9</wsse:Username> 
     <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">xxxxxxxx</wsse:Password> 
     </wsse:UsernameToken> 
    </Security> 
    </s:Header> 
    <s:Body> 
    <GenerateBarcode xmlns:d6p1="http://postnl.nl/cif/domain/BarcodeWebService/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://postnl.nl/cif/services/BarcodeWebService/"> 
     <d6p1:Message> 
     <d6p1:MessageID>5</d6p1:MessageID> 
     <d6p1:MessageTimeStamp>28-06-2017 14:15:41</d6p1:MessageTimeStamp> 
     </d6p1:Message> 
     <d6p1:Customer> 
     <d6p1:CustomerCode>DEVC</d6p1:CustomerCode> 
     <d6p1:CustomerNumber>11223344</d6p1:CustomerNumber> 
     </d6p1:Customer> 
     <d6p1:Barcode> 
     <d6p1:Type>3S</d6p1:Type> 
     <d6p1:Range>DEVC</d6p1:Range> 
     <d6p1:Serie>1000000-2000000</d6p1:Serie> 
     </d6p1:Barcode> 
    </GenerateBarcode> 
    </s:Body> 
</s:Envelope> 

,這似乎是關(同時這也是使用薩翁建設者BTW的情況下),最主要的是envelope的屬性和主操作的:generate_barcode形狀以及它的屬性。我不明白爲什麼我在GenerateBarcode之前得到前綴wsdl:

我告訴Savon採取我的xml和建立它完全一樣,但它不起作用。按原樣發送我的版本返回錯誤400。克里斯利用他的部分

編輯

使用@克里斯他的回答,我能夠做以下電話:

設置呼叫

@client = Savon.client(
       :endpoint    => 'https://testservice.postnl.com/CIF_SB/BarcodeWebService/1_1/BarcodeWebService.svc', 
       :wsdl     => 'https://testservice.postnl.com/CIF_SB/BarcodeWebService/1_1/?wsdl', 
       :log      => true, 
       :wsse_auth    => [ENV['postnl_username'], ENV['postnl_password']], 
       :pretty_print_xml  => true, 
       :convert_request_keys_to => :camelcase, 
       :env_namespace   => :s) 

    message = { 
        "d6p1:Message" => { 
        "d6p1:MessageID" => "7", 
        "d6p1:MessageTimeStamp" => I18n.l(Time.now, format: :postnl_api) 
       }, 
        "d6p1:Customer" => { 
        "d6p1:CustomerCode" => "DEVC", 
        "d6p1:CustomerNumber" => "11223344"}, 
        "d6p1:Barcode" => { 
         "d6p1:Type" => "3S", 
         "d6p1:Range" => "DEVC", 
         "d6p1:Serie" => "1000000-2000000" } 
       } 
    @client.call(:generate_barcode, :message => message, :soap_header => { "Action" => "http://postnl.nl/cif/services/BarcodeWebService/IBarcodeWebService/GenerateBarcode"}) 

呼叫

<?xml version="1.0" encoding="UTF-8"?> 
<s:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:wsdl="http://tempuri.org/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Header> 
    <Action>http://postnl.nl/cif/services/BarcodeWebService/IBarcodeWebService/GenerateBarcode</Action> 
    <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> 
     <wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="UsernameToken-1"> 
     <wsse:Username>devc_!R4xc8p9</wsse:Username> 
     <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">098fd559930983af31ef6630a0bb0c1974156561</wsse:Password> 
     </wsse:UsernameToken> 
    </wsse:Security> 
    </s:Header> 
    <s:Body> 
    <wsdl:GenerateBarcode> 
     <d6p1:Message> 
     <d6p1:MessageID>7</d6p1:MessageID> 
     <d6p1:MessageTimeStamp>17-07-2017 22:13:35</d6p1:MessageTimeStamp> 
     </d6p1:Message> 
     <d6p1:Customer> 
     <d6p1:CustomerCode>DEVC</d6p1:CustomerCode> 
     <d6p1:CustomerNumber>11223344</d6p1:CustomerNumber> 
     </d6p1:Customer> 
     <d6p1:Barcode> 
     <d6p1:Type>3S</d6p1:Type> 
     <d6p1:Range>DEVC</d6p1:Range> 
     <d6p1:Serie>1000000-2000000</d6p1:Serie> 
     </d6p1:Barcode> 
    </wsdl:GenerateBarcode> 
    </s:Body> 
</s:Envelope> 

響應

<?xml version="1.0"?> 
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Body> 
    <s:Fault> 
     <faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</faultcode> 
     <faultstring xml:lang="en-US">The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the &lt;serviceDebug&gt; configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs.</faultstring> 
    </s:Fault> 
    </s:Body> 
</s:Envelope> 
+0

你使用的是哪種版本的savon? – Chris

+0

版本2,你是否已經推薦3款? –

回答

1

好這個工程(認證)

@client = Savon.client(
      :wsdl     => 'https://testservice.postnl.com/CIF_SB/BarcodeWebService/1_1/?wsdl', 
      :log      => true, 
      :wsse_auth    => ['devc_!R4xc8p9', 'xxx'], 
      :pretty_print_xml  => true, 
      :convert_request_keys_to => :camelcase, 
      :env_namespace   => :s, 
      :namespace_identifier => nil 
      ) 

message = { 
       "d6p1:Message" => { 
       "d6p1:MessageID" => "10", 
       "d6p1:MessageTimeStamp" => Time.now.strftime("%d-%m-%Y %H:%M:%S") 
      }, 
       "d6p1:Customer" => { 
       "d6p1:CustomerCode" => "DEVC", 
       "d6p1:CustomerNumber" => "11223344"}, 
       "d6p1:Barcode" => { 
        "d6p1:Type" => "3S", 
        "d6p1:Range" => "DEVC", 
        "d6p1:Serie" => "1000000-2000000" } 
      } 


attributes = { "xmlns:d6p1" => "http://postnl.nl/cif/domain/BarcodeWebService/", 
       "xmlns:i" => "http://www.w3.org/2001/XMLSchema-instance", 
       "xmlns" => "http://postnl.nl/cif/services/BarcodeWebService/"} 

@client.call(:generate_barcode, :attributes => attributes, 
      :message => message, 
      :soap_header => { "Action" => "http://postnl.nl/cif/services/BarcodeWebService/IBarcodeWebService/GenerateBarcode"}) 

因此,關鍵是加入:namespace_identifier => nil和發送attributes。設置namespace_identifier會從GenerateBarcode中刪除wsdl,ans屬性會在GenerateBarcode標記上設置一些名稱空間。現在我記得爲什麼我非常討厭SOAP:(

+0

非常感謝你付出努力來研究我的問題。舊的答案我可以撥打電話,並獲得我在上面編輯的回覆,我的沙箱密碼哈希是'098fd559930983af31ef6630a0bb0c1974156561',不知道哪裏出問題了, –

+0

Ok更新了我的回答,這段代碼適合我! – Chris

+0

非常感謝你!不幸的是,這個賞金過期了,出於某種原因,我現在只能選擇100 ...對不起,無論如何,它已經過期。 –

相關問題