带有 XML 响应和正文的 SOAP 请求 swift 3,Alamofire

Posted

技术标签:

【中文标题】带有 XML 响应和正文的 SOAP 请求 swift 3,Alamofire【英文标题】:SOAP request with XML response and body swift 3, Alamofire 【发布时间】:2017-09-24 22:14:56 【问题描述】:

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

【问题讨论】:

试试这个:blastar.biz/2016/08/09/… Ty,伙计;)这可能是正确的答案。 很高兴为您提供帮助:) @KKRocks 链接无效,能否提供代码 【参考方案1】:

将此扩展用于 Alamofire。 AlamofireSoap

【讨论】:

@Shakeeb Manjeri 是否有添加基本身份验证的功能? 是的,alamofire 支持基本/NTLM 身份验证,这只是 alamofire 的扩展,因此它将支持 Alamofire 支持的所有功能【参考方案2】:

我使用了以下库

  pod 'Alamofire'
  pod 'SWXMLHash'
  pod 'AEXML'
  pod 'StringExtensionhtml'

那你需要导入

import SWXMLHash
import StringExtensionHTML
import AEXML
import Alamofire

这个功能对我有用

 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")
                
        

    

【讨论】:

收到错误,因为“类型 '[AEXMLElement]' 的值没有成员 'compactMap'”。请帮助。

以上是关于带有 XML 响应和正文的 SOAP 请求 swift 3,Alamofire的主要内容,如果未能解决你的问题,请参考以下文章

jQuery ajax SOAP 多部分响应pdf

Powershell SOAP 更新正文部分并重新请求

KSOAP 生成的 SOAP 请求不会导致任何错误但无法正常工作

使用Savon和Nokogiri在Rails中解析XML SOAP响应的内存不足

如何设置soap请求对象为 application/soap+xml;charset=utf-8

使用 org.apache.http 发送带有 SOAP 操作的 HTTP Post 请求