Http客户端POST请求xml + soap

时间:2012-12-05 22:50:14

标签: scala soap xmlhttprequest

我想在Scala中向此API发送帖子请求http://api.atinternet-solutions.com/toolbox/reporting.asmx 我第一次做这样的卷曲:

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以编程方式调用API

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)

但是我收到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"

post.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并获得了很好的内容。

我无法弄清楚出了什么问题。谢谢你的帮助

2 个答案:

答案 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正文仅包含query report示例中的占位符,而不是实际参数?

答案 1 :(得分:1)

我发现了什么问题。 替换

def postRequest 

通过

val postRequest.

再次发送