Advertisement
Guest User

Untitled

a guest
Mar 16th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. func soapCall(fonction:String, messageSoap:String) -> Int{
  2.  
  3.  
  4. if Reachability.isConnectedToNetwork() == true {
  5.  
  6. let soapRequest = AEXMLDocument()
  7. 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/"]
  8. let envelope = soapRequest.addChild(name: "SOAP-ENV:Envelope", attributes: attributes)
  9. let body = envelope.addChild(name: "SOAP-ENV:Body")
  10. let sendLead = body.addChild(name: "ns1:"+fonction)
  11. sendLead.addChild(name: "xml", attributes: ["xsi:type" : "xsd:string"], value: messageSoap)
  12.  
  13. let soapMessage = soapRequest.xmlString
  14.  
  15. let messageSoapMinify = minify(soapMessage)
  16.  
  17.  
  18.  
  19.  
  20. // URL Soap
  21. let urlString = soapServiceURL
  22. let url = NSURL(string: urlString)
  23. let theRequest = NSMutableURLRequest(URL: url!)
  24.  
  25. // Taille SoapMessage
  26. let msgLength = String(messageSoapMinify.characters.count)
  27.  
  28. // Soap Login
  29. let username = soapUsername
  30. let password = soapPassword
  31. let loginString = NSString(format: "%@:%@", username, password)
  32. let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!
  33. let base64LoginString = loginData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
  34.  
  35. // Requete Soap
  36. theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
  37. theRequest.addValue("Keep-Alive", forHTTPHeaderField: "Connection")
  38. theRequest.addValue(msgLength, forHTTPHeaderField: "Content-Length")
  39. theRequest.addValue("urn:ResponseAction", forHTTPHeaderField: "SOAPAction")
  40. theRequest.HTTPMethod = "POST"
  41. theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
  42. theRequest.addValue("Basic (base64LoginString)", forHTTPHeaderField: "Authorization")
  43. // print("Request is (theRequest.allHTTPHeaderFields!)")
  44.  
  45. let session = NSURLSession.sharedSession()
  46.  
  47. let task = session.dataTaskWithRequest(theRequest, completionHandler: {data, response, error -> Void in
  48. print("Response Login: (response)")
  49. let strData = NSString(data: data!, encoding: NSUTF8StringEncoding)!
  50. print("Body Login: (strData)")
  51. if error != nil
  52. {
  53. print("Error Login: " + error!.description)
  54. }
  55. let parser = NSXMLParser(data: data!)
  56. parser.delegate = self
  57. parser.parse()
  58. if(self.success == "1"){
  59. self.successSoap = 1
  60. }
  61.  
  62. })
  63.  
  64.  
  65. task.resume()
  66. }
  67.  
  68.  
  69. return successSoap
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement