ios - 带有 Soap Wcf Web 服务的 Swift ios

标签 ios swift

任何人都可以帮助解决 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/

相关文章:

ios - 如何让两个AIR IOS应用程序进行通信?

ios - 在 SwiftUI 中更改具有 NavigationView 和 ScrollView 的 TabView 的背景颜色

ios - 完全重置 CoreData

swift - 在闭包中初始化局部变量

ios - UIView 无法在具有自动布局的 UIScrollView 中正确居中

ios - 使用批处理在 Firestore 内增加值 - "Ambiguous use of ' 增量'"

iOS - 检索设备上访问次数最多的网站

string - 字符串 segue.identifier 与文字的 SWIFT 比较未按预期工作

ios - 通过 NSDate 查找数组索引?

ios - SwiftUI中的.transition修饰符