我试图在iOS中使用Swift查找有关如何使用SOAP服务的信息。我曾经习惯使用REST网络服务,但在这种情况下,我没有那么奢侈。我有wsdl。我需要使用的方法之一的请求如下所示:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fet="http://www.kyfb.com/ebuz/FetcheBuzAccountInfo/">
<soapenv:Header>
<wsse:Security soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken wsu:Id="SecurityToken-6138db82-5a4c-4bf7-915f-af7a10d9ae96" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:Username>ebusdoc</wsse:Username>
<wsse:Password>Ebusnes1</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<fet:fetchInformationForGuest>
<!--Optional:-->
<FetchInformationForGuestReq>
<entityNbr>0001354648</entityNbr>
<entityType>MBR</entityType>
</FetchInformationForGuestReq>
</fet:fetchInformationForGuest>
</soapenv:Body>
</soapenv:Envelope>
我知道如何在Swift中实际解析XML,但我不确定如何使用此请求获取响应。非常感谢任何帮助。
答案 0 :(得分:0)
open class HTTPSOAPClient {
open static let sharedInstance = HTTPSOAPClient()// Singleton instance yo use throughout the app
fileprivate init() {} //This prevents others from using the default '()' initializer for this class.
public func sendRequest(_ method: Alamofire.HTTPMethod,url: String, parameters: String, header: [String: String]?,completionHandler: @escaping (NSString)-> Void) {
var url = url
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let str:String = "<?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>" + parameters + "</soap:Body></soap:Envelope>"
let postData = NSData(data: str.data(using: String.Encoding.utf8)!)
var request = URLRequest(url: URL(string:url)!)
request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = postData as Data
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 {
}
let mutableData : Void = NSMutableData.initialize()
print(mutableData)
let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
completionHandler(dataString!)
print(dataString ?? "")
}
task.resume()
}
}
将sendRequest调用为
HTTPSOAPClient.sharedInstance.sendRequest(<#T##method: HTTPMethod##HTTPMethod#>, url: <#T##String#>, parameters: <#T##String#>, header: <#T##[String : String]?#>, completionHandler: <#T##(NSString) -> Void#>)