1

我想在 Scala 中向这个 API http://api.atinternet-solutions.com/toolbox/reporting.asmx发出一个 post 请求 我首先用这样的 curl 来做:

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 Internal Server Error 并打印 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 并得到了很好的内容。

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

4

2 回答 2

3

我用调度库试过你的电话。

这是我的代码

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
    }

}

服务回复像您一样的状态 500faultString ,但响应 xml 中的内容是:A parameter is missing in your NXHeader or you have a namespace issue.

这是您所期望的吗,因为post.txt正文仅包含查询报告示例中的占位符,而不是实际参数?

于 2012-12-06T09:45:57.387 回答
1

我发现出了什么问题。代替

def postRequest 

经过

val postRequest.

再次感谢

于 2012-12-06T16:56:16.700 回答