[英]SOAP request with XML response and body swift 3, Alamofire
I do not find any information about how to send SOAP
request on swift 3. I have a SOAP
API with XML
. 我没有找到有关如何在swift 3上发送
SOAP
请求的任何信息。我有一个带有XML
的SOAP
API。 I use Alamofire as the net client at my project. 我在项目中使用Alamofire作为网络客户端。 My question is How to make
SOAP
request with XML
on Swift, preferably with Alamofire library. 我的问题是如何在Swift上使用
XML
创建SOAP
请求,最好是使用Alamofire库。
I used following libraries 我使用了以下库
pod 'Alamofire'
pod 'SWXMLHash'
pod 'AEXML'
pod 'StringExtensionHTML'
Then you need to import 然后你需要导入
import SWXMLHash
import StringExtensionHTML
import AEXML
import Alamofire
This function works for me 这个功能对我有用
func getLines(){
let soapRequest = AEXMLDocument()
let envelopeAttributes = ["xmlns:soap":"http://schemas.xmlsoap.org/soap/envelope/", "xmlns:i" :"http://www.w3.org/2001/XMLSchema-instance","xmlns:d" : "http://www.w3.org/2001/XMLSchema","xmlns:c":"http://schemas.xmlsoap.org/soap/encoding/","xmlns:v":"http://schemas.xmlsoap.org/soap/envelope/"]
let act : AEXMLElement = AEXMLElement(name: "n0:getAllStations", value: "", attributes: ["id":"o0","c:root":"1", "xmlns:n0":"http://ws.wso2.org/dataservice"])
let child1 : AEXMLElement = AEXMLElement(name: "lang", value: "en", attributes: ["i:type":"d:string"])
act.addChild(child1)
let envelope = soapRequest.addChild(name: "soap:Envelope", attributes: envelopeAttributes)
envelope.addChild(name :"soap:Header")
let body = envelope.addChild(name: "soap:Body")
body.addChild(act)
let soapLenth = String(soapRequest.xml.characters.count)
let theURL = URL(string: "http://103.11.35.13:9080/services/RailwayWebServiceV2Proxy.RailwayWebServiceV2ProxyHttpSoap12Endpoint")
print(soapRequest.xml)
var mutableR = URLRequest(url: theURL!)
mutableR.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
mutableR.addValue(soapLenth, forHTTPHeaderField: "Content-Length")
mutableR.addValue("getAllStations", forHTTPHeaderField: "SOAPAction")
mutableR.httpMethod = "POST"
mutableR.httpBody = soapRequest.xml.data(using: String.Encoding.utf8)
Alamofire.request(mutableR)
.responseString { response in
if let xmlString = response.result.value {
let xml = SWXMLHash.parse(xmlString)
for element in xml["soapenv:Envelope"]["soapenv:Body"]["datas1:lines"]["datas1:line"].all {
if let nameElement = element["datas1:id"].element {
let line = Lines()
line.id = Int(nameElement.text)
self.lines.append(line)
}
if let nameElement = element["datas1:LineName"].element {
let line = Lines()
line.LineName = nameElement.text
self.lines.append(line)
}
}
}else{
print("error fetching XML")
}
}
}
use this extension to Alamofire. 将此扩展名用于Alamofire。 AlamofireSoap
AlamofireSoap
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.