gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:08:30 26 4
gpt4 key购买 nike

我正在尝试从 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com