Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement