2013-05-28 107 views
0

我試圖使用httpbuilder發出soap請求。我需要在頭部傳遞一些驗證參數。使用http build創建soap請求

我的代碼如下

def String WSDL_URL = 'http://ws.tradetracker.com/soap/affiliate?wsdl' 
    def http = new HTTPBuilder(WSDL_URL , ContentType.TEXT) 
    String soapEnvelope = 
       """<?xml version="1.0" encoding="utf-8"?> 
    <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
        xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> 
    <soap-env:Header> 
       <authenticate> 

       <customerID>id</customerID> 
       <passphrase>pass</passphrase> 
       <demo>true</demo> 
       </authenticate> 

    </soap-env:Header> 
     <soap12:Body> 
     <getConversionTransactions xmlns="xmlns':'http://schemas.xmlsoap.org/wsdl"> 
      <affiliateSiteID>id</affiliateSiteID> 


     </getConversionTransactions> 
     </soap12:Body> 
    </soap12:Envelope>""" 
      http.request(Method.POST, ContentType.TEXT) { 
       body = soapEnvelope 

       response.success = { resp, xml -> 
        String xm = xml.readLines() 
        println "XML was ${xm}" 

        def territories = new XmlSlurper().parseText(
         '<?xml version="1.0" encoding="UTF-8"?> <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="urn:http://ws.webgains.com/aws.php" xmlns:enc="http://www.w3.org/2003/05/soap-encoding" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><env:Body xmlns:rpc="http://www.w3.org/2003/05/soap-rpc"><ns1:getFullUpdatedEarningsResponse env:encodingStyle="http://www.w3.org/2003/05/soap-encoding"><rpc:result>return</rpc:result><return enc:itemType="ns1:fullLinesArray" enc:arraySize="1" xsi:type="ns1:fullReportArray"><item xsi:type="ns1:fullLinesArray"><transactionID xsi:type="xsd:int">39367137</transactionID><affiliateID xsi:type="xsd:int">59987</affiliateID><campaignName xsi:type="xsd:string">www.tikcode.com</campaignName><campaignID xsi:type="xsd:int">136755</campaignID><date xsi:type="xsd:dateTime">2013-05-13T15:04:48</date><validationDate xsi:type="xsd:dateTime">2013-05-13T15:04:48</validationDate><delayedUntilDate xsi:type="xsd:string"></delayedUntilDate><programName xsi:type="xsd:string">Miniinthebox - US</programName><programID xsi:type="xsd:int">4611</programID><linkID xsi:type="xsd:string">95661</linkID><eventID xsi:type="xsd:int">7285</eventID><commission xsi:type="xsd:float">0.06</commission><saleValue xsi:type="xsd:float">0.8</saleValue><status xsi:type="xsd:string">confirmed</status><paymentStatus xsi:type="xsd:string">notcleared</paymentStatus><changeReason xsi:nil="true"/><clickRef xsi:nil="true"/><clickthroughTime xsi:type="xsd:dateTime">2013-05-13T14:58:33</clickthroughTime><landingPage xsi:type="xsd:string">http%3A%2F%2Fwww.lightinthebox.com%2Fes%2F%3Flitb_from%3Daffiliate_webgains</landingPage><country xsi:type="xsd:string">ES</country><referrer xsi:type="xsd:string">http%3A%2F%2Flocalhost%3A8080%2Fcom.publidirecta.widget%2Fpromocion%2FverPromocion%3Fpromocion%3D</referrer></item></return></ns1:getFullUpdatedEarningsResponse></env:Body></env:Envelope>').declareNamespace("ns1":"http://ws.webgains.com/aws.php") 

       println "aaaaaaaaaaaaaaaa"+ territories.Body.getFullUpdatedEarningsResponse.return.item.transactionID 
       } 

       response.failure = { resp, xml -> 
        println "pues peto, no se porque"+xml.readLines() 
       } 
      } 

即時得到以下錯誤,我沒有任何線索想錯

<?xml version="1.0" encoding="UTF-8"?>, <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"><env:Body><env:Fault><env:Code><env:Value>env:Sender</env:Value></env:Code><env:Reason><env:Text>Body must be present in a SOAP envelope</env:Text></env:Reason></env:Fault></env:Body></env:Envelope> 

回答

1

命名空間信封和其對應的Header元素是不匹配的。

<soap12:Envelope>應該有<soap12:Header>而不是你有<soap-env:Header>。有效內容在標題元素中變爲無效,因此主體變得無法訪問。

+0

事實上這就是問題所在。謝謝你的回答 – user1485182

+0

很高興我能夠提供幫助。您可以接受對訪問該問題的其他人有幫助的答案。 – dmahapatro

1

就像@dmahapatro說你在XML中有問題。無論如何檢查你的代碼我已經注意到你直接使用HTTPBuilder。也許你可以嘗試使用groovy-wslite(https://github.com/jwagenleitner/groovy-wslite)來發出SOAP請求。調用和處理響應非常簡單。 Grails有一個插件,儘管我沒有使用插件,但直接使用groovy-wslite。

BuildConfig.groovy

dependencies { 

    compile 'com.github.groovy-wslite:groovy-wslite:0.7.2' 
    runtime 'com.github.groovy-wslite:groovy-wslite:0.7.2'  
} 

在一個Grails服務,例如:

def cnpj = "999999999906" 
    def clientSOAP = new SOAPClient('https://www.soawebservices.com.br/webservices/producao/cdc/cdc.asmx') 

    def response = clientSOAP.send (SOAPVersion.V1_2, 
     """<?xml version="1.0" encoding="utf-8"?> 
     <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> 
     <soap12:Body> 
      <PessoaJuridicaNFe xmlns="SOAWebServices"> 
      <Credenciais> 
       <Email>xxxxx</Email> 
       <Senha>xxxxx</Senha> 
      </Credenciais> 
      <Documento>${cnpj}</Documento> 
      </PessoaJuridicaNFe> 
     </soap12:Body> 
     </soap12:Envelope>""" 
    ) 

    //processing the response (very simple...) 
    Client client = new Client() 
    client.webServiceMsg = response.PessoaJuridicaNFeResponse.PessoaJuridicaNFeResult.Mensagem.text() 
    client.nome = response.PessoaJuridicaNFeResponse.PessoaJuridicaNFeResult.RazaoSocial.text() 
    //etc... 
+0

我嘗試過使用wslite(實際上,我使用wslite和其他Web服務並且像魅力一樣工作)。問題在於,這個特定的WS有0個文檔記錄,我試圖查看WSDL的所有內容都不工作,所以我想看看原始的XML,看看我能不能找出一些東西。 – user1485182