2012-12-05 37 views
0

我想在斯卡拉POST請求該API http://api.atinternet-solutions.com/toolbox/reporting.asmx 我第一次用這樣的捲曲做到了:HTTP客戶端POST請求的XML +肥皂

curl -X POST -T post.txt -H "Content-Type: application/soap+xml; charset=utf-8" http://api.atinternet-solutions.com/toolbox/reporting.asmx -v 

,我得到了我的預期。

現在我想用一個簡單的HttpClient

val httpClient = new DefaultHttpClient() 
def postRequest=new HttpPost("https://api.atinternet-solutions.com/toolbox/reporting.asmx") 
postRequest.addHeader("Content-Type","application/soap+xml ; charset=utf-8") 
postRequest.addHeader("SOAPAction","\"http://www.xiti.com/queryReport\"") 
val file=new File("post.txt") 
val fe=new FileEntity(file,"application/soap+xml;charset=utf-8") 
postRequest.setEntity(fe) 
val httpResponse=httpClient.execute(postRequest) 
println(httpResponse.getStatusLine.toString) 
val rspStr=Source.createBufferedSource(httpResponse.getEntity.getContent).mkString 
println(rspStr) 

編程調用API,但我得到一個HTTP/1.1 500內部服務器錯誤 和打印rspStr產生

"?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code><soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">Server was unable to process request. ---&gt; Root element is missing.</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope" 

職。 txt看起來像下面,除了我設置好信息。

<?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:Header> 
    <NXHeader xmlns="http://www.xiti.com/"> 
     <GUID>string</GUID> 
     <SecurityKey>string</SecurityKey> 
     <Site>int</Site> 
    </NXHeader> 
    </soap12:Header> 
    <soap12:Body> 
    <queryReport xmlns="http://www.xiti.com/"> 
     <startDate>int</startDate> 
     <endDate>int</endDate> 
     <query>string</query> 
     <param>string</param> 
     <typereport>XML or CSV</typereport> 
    </queryReport> 
    </soap12:Body> 
</soap12:Envelope> 

正在工作的curl動作是從我的scala項目目錄完成的。我在scala上打印了post.txt並獲得了很好的內容。

我想不出什麼錯誤。感謝您的幫助

+0

什麼post.txt的內容替換

def postRequest 

? – Brian

+0

你有沒有正確的道路post.txt?嘗試打開它並從Scala閱讀。 – Jakozaur

+0

@Jakozaur我在scala中打開post.txt並閱讀它。它工作正常。 –

回答

1

我發現了什麼事錯了。 再次

val postRequest. 

韓國社交協會

+0

不錯......如果你願意,你可以接受你的答案 –

3

我試圖與dispatch library你的電話。

這是我的代碼

import dispatch._ 
import java.io.File 

object ToolBox { 

    val endpoint = url("https://api.atinternet-solutions.com/toolbox/reporting.asmx").POST 
     .addHeader("Content-Type","application/soap+xml ; charset=utf-8") 
     .addHeader("SOAPAction",""" "http://www.xiti.com/queryReport" """) 

    def report = Http(endpoint <<< new File("post.txt") > as.xml.Elem) 

    def tryReport = { 
     val res = report.either 
     for { 
      ex <- res.left 
     } yield "Something got wrong " + ex.getMessage 
    } 

} 

的服務與狀態500像你這樣的回答,但在響應XML的faultString是:A parameter is missing in your NXHeader or you have a namespace issue.

是不是你所期望的,因爲post.txt body僅包含來自query report示例的佔位符,而不是實參?

+0

我在我的post.txt中有真實的參數,但其中的一部分是訪問令牌,因此我無法在線發佈它們。其實我發現問題出在哪裏(看我的答案)。感謝您的幫助。 –