函数中的dataTaskWithRequest(soap)

时间:2016-03-16 15:37:55

标签: ios swift soap

我正在进行SOAP调用,但是当我调试时,但是当我执行task.resume()时,它并没有在第一次进入dataTaskWithRequest,所以我继续调试,它只是在没有调用的情况下进入dataTaskWithRequest的最后,我不明白它为什么会这样做,这是因为我将它用于函数中?代码:

func soapCall(fonction:String, messageSoap:String) -> Int{


    if Reachability.isConnectedToNetwork() == true {

        let soapRequest = AEXMLDocument()
        let attributes = ["xmlns:SOAP-ENV" : "http://schemas.xmlsoap.org/soap/envelope/", "xmlns:ns1" : "urn:contactFile", "xmlns:xsd" : "http://www.w3.org/2001/XMLSchema", "xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance", "xmlns:SOAP-ENC" : "http://schemas.xmlsoap.org/soap/encoding/", "SOAP-ENV:encodingStyle" : "http://schemas.xmlsoap.org/soap/encoding/"]
        let envelope = soapRequest.addChild(name: "SOAP-ENV:Envelope", attributes: attributes)
        let body = envelope.addChild(name: "SOAP-ENV:Body")
        let sendLead = body.addChild(name: "ns1:"+fonction)
        sendLead.addChild(name: "xml", attributes: ["xsi:type" : "xsd:string"], value: messageSoap)

        let soapMessage = soapRequest.xmlString

        let messageSoapMinify = minify(soapMessage)




        //        URL Soap
        let urlString = soapServiceURL
        let url = NSURL(string: urlString)
        let theRequest = NSMutableURLRequest(URL: url!)

        //        Taille SoapMessage
        let msgLength = String(messageSoapMinify.characters.count)

        //        Soap Login
        let username = soapUsername
        let password = soapPassword
        let loginString = NSString(format: "%@:%@", username, password)
        let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!
        let base64LoginString = loginData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)

        //        Requete Soap
        theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
        theRequest.addValue("Keep-Alive", forHTTPHeaderField: "Connection")
        theRequest.addValue(msgLength, forHTTPHeaderField: "Content-Length")
        theRequest.addValue("urn:ResponseAction", forHTTPHeaderField: "SOAPAction")
        theRequest.HTTPMethod = "POST"
        theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
        theRequest.addValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
        //        print("Request is \(theRequest.allHTTPHeaderFields!)")

        let session = NSURLSession.sharedSession()

        let task = session.dataTaskWithRequest(theRequest, completionHandler: {data, response, error -> Void in
            print("Response Login: \(response)")
            let strData = NSString(data: data!, encoding: NSUTF8StringEncoding)!
            print("Body Login: \(strData)")
            if error != nil
            {
                print("Error Login: " + error!.description)
            }
            let parser = NSXMLParser(data: data!)
            parser.delegate = self
            parser.parse()
            if(self.success == "1"){
                self.successSoap = 1
            }

        })


        task.resume()
    }


    return successSoap
}

我成功收到了数据库中的数据,但是这里他们发现了task.resume()的错误,它不会立即进入dataTaskWithRequest()。

编辑:

func soapCall(fonction:String, messageSoap:String, completion: (result: Int)->()){
    ...
        let task = session.dataTaskWithRequest(theRequest, completionHandler: {data, response, error -> Void in
            print("Response Login: \(response)")
            let strData = NSString(data: data!, encoding: NSUTF8StringEncoding)!
            print("Body Login: \(strData)")
            if error != nil
            {
                print("Error Login: " + error!.description)
            }
            let parser = NSXMLParser(data: data!)
            parser.delegate = self
            parser.parse()
            if(self.success == "1"){
                self.successSoap = 1
            }

        })


        task.resume()
    }
    completion(result: successSoap)
}

这里的电话:

    var statutEnvoie = 0

    serviceSoap.soapCall(fonctionSoap, messageSoap: messageSoapSendLead){ (result) in
        if result == 1 {
            print("SoapCall OK !!!!!!!!!!!!!")
            statutEnvoie = 1
        } else {
            print("SoapCall PAS BON !!!!!!!!!!!!!!!!!")
            statutEnvoie = 0
        }
    }

0 个答案:

没有答案