任何人都可以帮助解决 swift ios 与 soap 对象的连接问题
var soapMessage = "<?xml version='1.0' encoding='UTF-8'?><soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><soap:Body> <BindCategory xmlns=‘http://tempuri.org/'></BindCategory></soap:Body></soap:Envelope>"
var urlString = "http://assetwebservice.sudesi.in/service.svc"
var msgLength = String(count(soapMessage))
var url = NSURL(string: urlString)!
var theRequest = NSMutableURLRequest(URL: url)
theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
theRequest.addValue("http://tempuri.org/IService/BindCategory", forHTTPHeaderField: "Soapaction")
theRequest.addValue(msgLength, forHTTPHeaderField: "Content-Length")
theRequest.HTTPMethod = "POST"
theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
var connection = NSURLConnection(request: theRequest, delegate: self,startImmediately: true)
connection?.start()
if (connection == true) {
println("Connection success")
var mutableData : Void = NSMutableData.initialize()
}else{
println("Error in connection")
}
最佳答案
好吧,我突然想到的是:
if (connection == true) {
println("Connection success")
// What does this line mean? I can't make sense of it.
var mutableData : Void = NSMutableData.initialize()
}
您正在将 NSURLConnection?
与 Bool
进行比较。这永远不会是真的。此外,这不是判断连接是否成功的正确方法。 NSURLConnection
有一个可选的初始值设定项,因此它可以检查其参数是否有效,而不是确定连接是否成功。成功将由对委托(delegate)的适当回调决定。
你应该考虑使用 NSURLSession
而不是 NSURLConnection
除非你必须支持 iOS < 7.0,我对此表示怀疑。我怀疑——也许是错误的——提问者是一个正在做 Swift 的 C# 程序员,他可能不熟悉委托(delegate)模式,这种模式在 Apple 的框架中很常见,但在 Microsoft 的框架中并不常见。如果是这样,NSURLSession
的界面将更加可口。
关于ios - 带有 Soap Wcf Web 服务的 Swift ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31199636/