首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用XML响应和正文swift 3的SOAP请求,Alamofire

使用XML响应和正文swift 3的SOAP请求,Alamofire
EN

Stack Overflow用户
提问于 2017-04-27 16:19:17
回答 2查看 4.6K关注 0票数 3

我没有找到任何关于如何在swift 3上发送SOAP请求的信息。我有一个带有XMLSOAP API。我在我的项目中使用Alamofire作为网络客户端。我的问题是如何在Swift上用XML发出SOAP请求,最好是用Alamofire库。

EN

回答 2

Stack Overflow用户

发布于 2017-10-18 22:59:32

我使用了以下库

代码语言:javascript
运行
复制
  pod 'Alamofire'
  pod 'SWXMLHash'
  pod 'AEXML'
  pod 'StringExtensionHTML'

然后,您需要导入

代码语言:javascript
运行
复制
import SWXMLHash
import StringExtensionHTML
import AEXML
import Alamofire

此函数适用于我

代码语言:javascript
运行
复制
 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")
                }
        }

    }
票数 3
EN

Stack Overflow用户

发布于 2019-04-08 22:08:38

使用此扩展到Alamofire。AlamofireSoap

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43652331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档