Как использовать urlsession с swift3 XMLParse
Хорошая работа. Я Тунец. Сервис SOAP подключается следующим образом:
//<- Соединение -> ***************************************** *********************
let xml = String(format:"<?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><Giris xmlns='http://atauni.edu.tr/'><strUserName>%@</strUserName><strPassword>%@</strPassword><TC>%@</TC><Pass>%@</Pass><DeviceID>%@</DeviceID></Giris></soap12:Body></soap12:Envelope>",strusername,strpassword,tc!,password,deviceid!)
let soapMessage = xml
let msgLength = String(describing: soapMessage.characters.count)
let url = URL(string: "http://obs.atauni.edu.tr")
var request = URLRequest(url: url!)
request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.addValue(msgLength, forHTTPHeaderField: "Content-Length")
request.httpMethod = "POST"
request.httpBody = soapMessage.data(using: String.Encoding.utf8, allowLossyConversion: false)
let session = URLSession.shared
let task = session.dataTask(with: request) { (data, resp, error) in
if data == nil {
print("data hata error: \(String(describing: error))")
return
}
let mutableData : Void = NSMutableData.initialize()
//print(mutableData)
var dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
self.mutableData.append(data!)
print("***************************")
print(dataString ?? String())
print("***************************")
}
task.resume()
//<!--Connection--> **************************************************************
Я также сделал парсер XML следующим образом:
Прошу прощения за плохой английский
1 ответ
Решение
Задача решена
//Urlsession с swift3 XMLParse
func Giris (tc:String, pass:String)-> String {
let xml = String(format:"<?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><Giris xmlns=''><strUserName>%@</strUserName><strPassword>%@</strPassword><TC>%@</TC><Pass>%@</Pass><DeviceID>%@</DeviceID></Giris></soap12:Body></soap12:Envelope>",self.strusername,self.strpassword,tc,pass,deviceid!)
let soapMessage = xml
let msgLength = String(describing: soapMessage.characters.count)
let url = URL(string: "link")
var request = URLRequest(url: url!)
request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.addValue(msgLength, forHTTPHeaderField: "Content-Length")
request.httpMethod = "POST"
request.httpBody = soapMessage.data(using: String.Encoding.utf8, allowLossyConversion: false)
let session = URLSession.shared
let task = session.dataTask(with: request) { (data, resp, error) in
guard error == nil && data != nil else{
print("Error:",error!)
return
}
let _ : Void = NSMutableData.initialize()
print(self.mutableData)
let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
self.mutableData.append(data!)
self.dtStrng_giris = self.jsoncreate_giris(xml: dataString! as String)
if let jsonlist = self.convertToDictionary_giris (text: self.dtStrng_giris) as? [AnyObject]{
for i in jsonlist as! [[String: AnyObject]]{
self.id = i["ID"] as! String
}
}
}; task.resume()
return ""
}
func convertToDictionary_giris(text:String)-> Any?{
if let data = text.data(using: .utf8){
do{
return try JSONSerialization.jsonObject(with: data, options: []) as? Any
} catch{
print(error.localizedDescription)
}
}
return nil
}
func jsoncreate_giris (xml : String)-> String {
if let start = xml.range(of: "["),
let end = xml.range(of: "]", range: start.upperBound..<xml.endIndex) {
let substring = xml[start.upperBound..<end.lowerBound]
return jsonsonuc_giris(a: substring)
}
else {
print("invalid input")
return ""
}
}
func jsonsonuc_giris(a:String)-> String {
return "[" + a + "]"
}