ios - 在 Swift iPhone 应用程序中请求 SOAP Web 服务时 XMLStreamreader 错误

标签 ios iphone swift soap

我正在尝试从 Swift 代码中使用 SOAP Web 服务,但收到以下响应:

Optional(<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Body><soap:Fault><soap:Code><soap:Value>soap:Sender</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">Error reading XMLStreamReader: Unexpected EOF in prolog at [row,col {unknown-source}]: [1,38]</soap:Text></soap:Reason></soap:Fault></soap:Body></soap:Envelope>)

SOAP 请求:

var soapMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
    "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\""
    "xmlns:v1=\"http://xxx/SecurityServiceWSDLType/V1\""
    "xmlns:v11=\"http://xxx/Security/V1\">"
    "<soap:Header/>"
    "<soap:Body>"
    "<v1:Authenticate>"
    "<v1:UserLoginDetails>"
    "<v11:UserId>aaa</v11:UserId>"
    "<v11:Password>aaa</v11:Password>"
    "</v1:UserLoginDetails>"
    "</v1:Authenticate>"
    "</soap:Body>"
    "</soap:Envelope>"

    //var soapMessage = text
    var url = NSURL(string: wsUrl)
    var theRequest = NSMutableURLRequest(URL: url!)
    var msgLength = String(count(soapMessage))

    theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
    theRequest.addValue(msgLength, forHTTPHeaderField: "Content-Length")
    theRequest.HTTPMethod = "POST"
    theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) // or false

    var connection = NSURLConnection(request: theRequest, delegate: self, startImmediately: true)
    connection?.start()

    if(connection == true){
        var mutableData : Void = NSMutableData.initialize()
    }

为什么我会收到此错误?我已经在 Chrome 中使用 postmaster 插件检查了 SOAP 请求,它工作正常,但当我从 iOS 代码访问它时,我遇到了上述错误。

最佳答案

如果打印出 soapMessage,您将看到它只有序言:

println(soapMessage)

结果

<?xml version="1.0" encoding="utf-8"?>

您需要连接字符串:

var soapMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
  + "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\""
  + "xmlns:v1=\"http://xxx/SecurityServiceWSDLType/V1\""
  + "xmlns:v11=\"http://xxx/Security/V1\">"
  + "<soap:Header/>"
  + "<soap:Body>"
  + "<v1:Authenticate>"
  + "<v1:UserLoginDetails>"
  + "<v11:UserId>aaa</v11:UserId>"
  + "<v11:Password>aaa</v11:Password>"
  + "</v1:UserLoginDetails>"
  + "</v1:Authenticate>"
  + "</soap:Body>"
  + "</soap:Envelope>"

关于ios - 在 Swift iPhone 应用程序中请求 SOAP Web 服务时 XMLStreamreader 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32049242/

相关文章:

ios - 在 iOS 9.2.1/XCode 7.2 中执行 segue 时偶尔出现黑屏

ios - Braintree v.zero iOS SDK 是否支持自适应支付?

ios - 标准Parse.com设置会发出警告“正在执行长时间运行的操作……”

swift - AudioQueueBuffers 之间的爆音

ios - 益智游戏中的某些 UIImageViews 不会移动到新的 CGPoint 中心

iphone - TableView 不会加载到 UINavigationController 内部

iphone - 有没有一种简单的方法可以在 iOS 中将货币格式化为字符串?

ios - 单击按钮时如何使用用户当前位置显示注释?

ios - 在解包可选时发现 nil

iphone - 从 Xcode 4.6.2 部署到 iOS 7(测试版)设备