2017-01-18 5 views

Antwort

0
static func callAPI(strUrl: String, soapMsg : String ,param: NSDictionary?, mediaData : NSData? , strAction : String,completion : @escaping (JSON) ->()){ 

    UIApplication.shared.beginIgnoringInteractionEvents() 

    let soapLenth = String(soapMsg.characters.count) 
    let theURL = NSURL(string: strUrl) 
    let mutableR = NSMutableURLRequest(url: theURL! as URL) 

    mutableR.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type") 
    mutableR.addValue("text/html; charset=utf-8", forHTTPHeaderField: "Content-Type") 
    // mutableR.addValue("application/json", forHTTPHeaderField: "Accept") 
    mutableR.addValue("www.w3schools.com", forHTTPHeaderField: "Host") 
    mutableR.addValue(soapLenth, forHTTPHeaderField: "Content-Length") 
    mutableR.addValue("https://tempuri.org/"+"\(strAction)", forHTTPHeaderField: "SOAPAction") 
    mutableR.httpMethod = "POST" 
    mutableR.httpBody = soapMsg.data(using: String.Encoding.utf8) 

    let manager = AFHTTPSessionManager() 
    manager.requestSerializer.timeoutInterval=180 
    manager.responseSerializer=AFHTTPResponseSerializer() 
    // manager.responseSerializer.acceptableContentTypes=NSSet(array: ["text/xml"]) as? Set<String> 

    let task = manager.dataTask(with: mutableR as URLRequest, uploadProgress: nil, downloadProgress: nil) { (response, responseObject, error) in 

     if (error == nil){ 
      let str=NSString(data: responseObject as! Data, encoding: String.Encoding.utf8.rawValue) 
      let data = str!.components(separatedBy: "<?")[0].data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))! 
      jsonData=JSON(data: data) 
      MBProgressHUD.hide(for: view, animated: true) 
      UIApplication.shared.endIgnoringInteractionEvents() 
      if jsonData != nil{ 

       completion(jsonData) 
      } 
     } 
     else{ 
      MBProgressHUD.hide(for: view, animated: true) 
      self.view.makeToast(error!.localizedDescription) 
      UIApplication.shared.endIgnoringInteractionEvents() 
      completion(JSON(data: NSData() as Data)) 
     } 
    } 
    task.resume() 
} 

Aufruf über Funktion

let soapMessage = NSString(format: "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><FahrenheitToCelsius xmlns=\"https://tempuri.org/\"><Fahrenheit>%@</Fahrenheit></FahrenheitToCelsius></soap:Body></soap:Envelope>", "25") 

APIS.callAPI(strUrl: "http://www.w3schools.com/xml/tempconvert.asmx", soapMsg: soapMessage as String, param: nil, mediaData : nil ,strAction: "FahrenheitToCelsius", completion: { (JSON) in 
    print(JSON) 
}) 
+0

Amit jagesha oki wird es versuchen. – Arun

+0

amit jagesh. Ich habe meinen Code, ich muss Benutzernamen und Passwort weitergeben meine Ausgabe ist nicht JASON ich bin als XML.please versuchen, meinen neuen Code, den ich angehängt habe. Ich habe keine Ausgabe – Arun

0
@IBAction func actionConvert(_ sender : AnyObject) { 

    let soapMessage = "<?xml version='1.0' encoding='utf-8'?><soap12:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap12='http://www.w3.org/2003/05/soap-envelope'><soap12:Body><TD_UserLogin xmlns='http://xxxxxxxxxxxxxxxxxxx.com/xml/'><LoginName>\(usernam)</LoginName><Password>\(pass)</Password></TD_UserLogin></soap12:Body></soap12:Envelope>" 

    let urlString = "https://xxxxxxxxxxxxxxxxxxxxxx.asmx" 

    let url = URL(string: urlString) 

    let theRequest = NSMutableURLRequest(url: url!) 

    let msgLength = soapMessage.characters.count 

    theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type") 
    theRequest.addValue(String(msgLength), forHTTPHeaderField: "Content-Length") 
    theRequest.httpMethod = "POST" 
    theRequest.httpBody = soapMessage.data(using: String.Encoding.utf8, allowLossyConversion: false) // or false 

    let connection = NSURLConnection(request: theRequest as URLRequest, delegate: self, startImmediately: true) 
    connection!.start() 

    print("soap message is = \(soapMessage)") 


    if (connection != nil) { 
     var mutableData : Void = NSMutableData.initialize() 
     print("Problem") 
    } 
    else { 
     print("theConnection is NULL") 
    } 

} 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


// NSURLConnectionDelegate 

// NSURL 



func connection(_ connection: NSURLConnection!, didReceiveResponse response: URLResponse!) { 
    mutableData.length = 0; 
} 

func connection(_ connection: NSURLConnection!, didReceiveData data: Data!) { 
    mutableData.append(data) 
} 


func connectionDidFinishLoading(_ connection: NSURLConnection!) { 
    let response = NSString(data: mutableData as Data, encoding: String.Encoding.utf8.rawValue) 

    let xmlParser = XMLParser(data: mutableData as Data) 
    xmlParser.delegate = self 
    xmlParser.parse() 
    xmlParser.shouldResolveExternalEntities = true 
} 


// NSXMLParserDelegate 


func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) { 
    currentElementName = elementName as NSString 
    print("curent element = \(currentElementName)") 
} 


func parser(_ parser: XMLParser, foundCharacters string: String) { 
    if currentElementName == "TD_AuthenticateUserResponse" { 
     //log 
    } 
} 
} 
0

lesen http://webindream.com/soap-with-swift/

Sie haben gerade von NSURLConnection zu NSURLSession in Swift ändern 3

+0

ja ich tat, aber nicht analysieren kann mein geänderter Code Ich habe angebracht – Arun