答案 0 :(得分:3)
不使用ConnectionDataDelegate
尝试此操作func servisRun(xml:String!){
let soapMessage = xml
let msgLength = String(describing: soapMessage?.characters.count)
let url = URL(string: "http....")
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("connection error or data is nill")
return
}
if resp != nil {
self.mutableData.length = 0;
}
let mutableData : Void = NSMutableData.initialize()
print(mutableData)
let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
self.mutableData.append(data!)
print(dataString)
}
task.resume()
let xmlParser = XMLParser(data: mutableData as Data)
xmlParser.delegate = self
xmlParser.parse()
xmlParser.shouldResolveExternalEntities = true
}