【问题标题】:Http client POST request xml+soapHttp客户端POST请求xml+soap
【发布时间】:2012-12-07 00:56:16
【问题描述】:

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

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

我不知道出了什么问题。谢谢你的帮助

【问题讨论】:

  • post.txt的内容是什么?
  • 你有正确的 post.txt 路径吗?尝试打开它并从 Scala 中读取。
  • @Jakozaur 我在 scala 中打开了 post.txt 并阅读了它。它工作正常。
  • @Brian 我将 post.txt 的内容添加到我的问题中
  • 你试过用java代码测试吗?也许问题与 HTTPClient 库有关。

标签: scala soap xmlhttprequest


【解决方案1】:

我尝试使用 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
    }

}

服务回复 status 500,但响应 xml 中的 faultString 是:A parameter is missing in your NXHeader or you have a namespace issue.

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

【讨论】:

  • 我的 post.txt 中有真实参数,但其中一部分是访问令牌,因此我无法在线发布它们。实际上我找到了问题所在(请参阅我的答案)。感谢您的帮助。
【解决方案2】:

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

def postRequest 

val postRequest.

再次感谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多