以下是我用集成密钥,用户名和密码向我的Web服务发送SOAP消息的代码。我能够得到响应并将其解析为foundCharacters。在swift中处理XML解析的响应
现在我需要存储在解析的响应中找到的两个元素,以便稍后可以使用它们来处理另一个请求。
我一直在寻找各地的教程,但我不能安静地理解这些教程,因为它们大多数都是关于XML文件本地存储而不是来自真正的WebService。
class LoginCentralViewController: UIViewController, XMLParserDelegate, NSURLConnectionDelegate {
var chaveWS = ChaveWebService().chave()
var mutableData:NSMutableData = NSMutableData()
var currentElement:NSString = ""
@IBAction func btnAcessarACTION(_ sender: Any) {
let soapMessage = "<soapenv:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:log='LoginCentral'><soapenv:Header/><soapenv:Body><log:LoginCentral soapenv:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'><Autenticacao xsi:type='urn:Autenticacao' xmlns:urn='urn:RouterBoxMobile'><ChaveIntegracao xsi:type='xsd:string'>\(chaveWS)</ChaveIntegracao></Autenticacao><DadosLoginCentral xsi:type='urn:DadosLoginCentral' xmlns:urn='urn:RouterBoxMobile'><Usuario xsi:type='xsd:string'>wagner</Usuario><Senha xsi:type='xsd:string'>mudar123</Senha></DadosLoginCentral></log:LoginCentral></soapenv:Body></soapenv:Envelope>"
let urlString = "https://example.com?wsdl"
let url = NSURL (string: urlString)
let theRequest = NSMutableURLRequest(url: url! as 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)
let connection = NSURLConnection(request: theRequest as URLRequest, delegate: self, startImmediately: true)
connection!.start()
if (connection != nil) {
var mutableData : Void = NSMutableData.initialize()
}
print("passou")
}
override func viewDidLoad() {
super.viewDidLoad()
}
func connection(_ connection: NSURLConnection!, didReceiveResponse response: URLResponse!) {
mutableData.length = 0;
print("passou aqui tbm")
}
func connection(_ connection: NSURLConnection!, didReceiveData data: NSData!) {
mutableData.append(data as 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
//print(response)
}
//XMLParser
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
currentElement = elementName as NSString
//print(elementName)
}
func parser(_ parser: XMLParser, foundCharacters string: String) {
if currentElement == "LoginCentralResponse" {
print(currentElement, string)
}
print(currentElement, string)
}
}
这里是我需要保存和再利用解析响应:
ClientCode : 8
Permissions : 70,73,77,75,71,72
我对常量有一些疑问。 public let verify_token =“VerifyToken”<<我有一个安全令牌来授权连接,但它必须在soap消息中发送,就像userName和password一样,还是我误解了它? –
不,这些变量只是一个让你理解的例子,包括SOAP消息。 –
在SOAP中,有SOAPAction,BASE_URL和WSDL_URL的权利吗?这就是为什么我让它声明为常量。让我简单回顾一下 –